home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / gccdif~1.z / gccdif~1 / cccp.c next >
Encoding:
C/C++ Source or Header  |  1989-09-30  |  145.6 KB  |  5,797 lines

  1. /* C Compatible Compiler Preprocessor (CCCP)
  2. Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  3.                     Written by Paul Rubin, June 1986
  4.             Adapted to ANSI C, Richard Stallman, Jan 1987
  5.  
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 1, or (at your option) any
  9. later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  In other words, you are welcome to use, share and improve this program.
  21.  You are forbidden to forbid anyone else to use, share and improve
  22.  what you give them.   Help stamp out software-hoarding!  */
  23.  
  24. typedef unsigned char U_CHAR;
  25.  
  26. #ifndef atarist
  27.  
  28. #ifdef EMACS
  29. #define NO_SHORTNAMES
  30. #include "../src/config.h"
  31. #ifdef open
  32. #undef open
  33. #undef read
  34. #undef write
  35. #endif /* open */
  36. #endif /* EMACS */
  37.  
  38. #ifndef EMACS
  39. #include "config.h"
  40. #endif /* not EMACS */
  41.  
  42. /* In case config.h defines these.  */
  43. #undef bcopy
  44. #undef bzero
  45. #undef bcmp
  46.  
  47. #include <sys/types.h>
  48. #include <sys/stat.h>
  49. #include <ctype.h>
  50. #include <stdio.h>
  51. #include <signal.h>
  52.  
  53. #ifndef VMS
  54. #include <sys/file.h>
  55. #if (!(defined(USG) || defined(atariminix)))
  56. #include <sys/time.h>        /* for __DATE__ and __TIME__ */
  57. #include <sys/resource.h>
  58. #else
  59. #ifndef atariminix
  60. #define index strchr
  61. #define rindex strrchr
  62. #endif
  63. #include <time.h>
  64. #include <fcntl.h>
  65. #endif /* USG or atariminix */
  66. #endif /* not VMS */
  67.  
  68. #else        /* atarist */
  69.  
  70. #include <ctype.h>
  71. #include <stdio.h>
  72. #include <types.h>
  73. #include <stat.h>
  74. #include <file.h>
  75. #include <time.h>
  76. #include "stddef.h"
  77.  
  78. extern long _stksize = 524288;    /* want big stack cause include files
  79.                    get alloca'ed there */
  80.  
  81. #endif        /* atarist */
  82.  
  83.  
  84. /* VMS-specific definitions */
  85. #ifdef VMS
  86. #include <time.h>
  87. #include <errno.h>        /* This defines "errno" properly */
  88. #include <perror.h>        /* This defines sys_errlist/sys_nerr properly */
  89. #define O_RDONLY    0    /* Open arg for Read/Only  */
  90. #define O_WRONLY    1    /* Open arg for Write/Only */
  91. #define read(fd,buf,size)    VAX11_C_read(fd,buf,size)
  92. #define write(fd,buf,size)    VAX11_C_write(fd,buf,size)
  93. #ifdef __GNUC__
  94. #define BSTRING            /* VMS/GCC supplies the bstring routines */
  95. #endif /* __GNUC__ */
  96. #endif /* VMS */
  97.  
  98. #define max(a,b) ((a) > (b) ? (a) : (b))
  99.  
  100. /* External declarations.  */
  101.  
  102. void bcopy (), bzero ();
  103. int bcmp ();
  104. extern char *getenv ();
  105. extern char *version_string;
  106.  
  107. /* Forward declarations.  */
  108.  
  109. struct directive;
  110. struct file_buf;
  111. struct arglist;
  112. struct argdata;
  113.  
  114. int do_define (), do_line (), do_include (), do_undef (), do_error (),
  115.   do_pragma (), do_if (), do_xifdef (), do_else (),
  116.   do_elif (), do_endif (), do_sccs (), do_once ();
  117.  
  118. struct hashnode *install ();
  119. struct hashnode *lookup ();
  120.  
  121. char *xmalloc (), *xrealloc (), *xcalloc (), *savestring ();
  122. void fatal (), fancy_abort (), pfatal_with_name (), perror_with_name ();
  123.  
  124. void macroexpand ();
  125. void dump_all_macros ();
  126. void conditional_skip ();
  127. void skip_if_group ();
  128. void output_line_command ();
  129. /* Last arg to output_line_command.  */
  130. enum file_change_code {same_file, enter_file, leave_file};
  131.  
  132. int grow_outbuf ();
  133. int handle_directive ();
  134. void memory_full ();
  135.  
  136. U_CHAR *macarg1 ();
  137. char *macarg ();
  138.  
  139. U_CHAR *skip_to_end_of_comment ();
  140. U_CHAR *skip_quoted_string ();
  141.  
  142. #ifndef FATAL_EXIT_CODE
  143. #define FATAL_EXIT_CODE 33    /* gnu cc command understands this */
  144. #endif
  145.  
  146. #ifndef SUCCESS_EXIT_CODE
  147. #define SUCCESS_EXIT_CODE 0    /* 0 means success on Unix.  */
  148. #endif
  149.  
  150. /* Name under which this program was invoked.  */
  151.  
  152. char *progname;
  153.  
  154. /* Nonzero means handle C++ comment syntax and use
  155.    extra default include directories for C++.  */
  156.  
  157. int cplusplus;
  158.  
  159. /* Current maximum length of directory names in the search path
  160.    for include files.  (Altered as we get more of them.)  */
  161.  
  162. int max_include_len;
  163.  
  164. /* Nonzero means copy comments into the output file.  */
  165.  
  166. int put_out_comments = 0;
  167.  
  168. /* Nonzero means don't process the ANSI trigraph sequences.  */
  169.  
  170. int no_trigraphs = 0;
  171.  
  172. /* Nonzero means print the names of included files rather than
  173.    the preprocessed output.  1 means just the #include "...",
  174.    2 means #include <...> as well.  */
  175.  
  176. int print_deps = 0;
  177.  
  178. /* Nonzero means don't output line number information.  */
  179.  
  180. int no_line_commands;
  181.  
  182. /* Nonzero means inhibit output of the preprocessed text
  183.    and instead output the definitions of all user-defined macros
  184.    in a form suitable for use as input to cccp.  */
  185.  
  186. int dump_macros;
  187.  
  188. /* Nonzero means give all the error messages the ANSI standard requires.  */
  189.  
  190. int pedantic;
  191.  
  192. /* Nonzero means warn if slash-star appears in a comment.  */
  193.  
  194. int warn_comments;
  195.  
  196. /* Nonzero means warn if there are any trigraphs.  */
  197.  
  198. int warn_trigraphs;
  199.  
  200. /* Nonzero means try to imitate old fashioned non-ANSI preprocessor.  */
  201.  
  202. int traditional;
  203.  
  204. /* Nonzero causes output not to be done,
  205.    but directives such as #define that have side effects
  206.    are still obeyed.  */
  207.  
  208. int no_output;
  209.  
  210. /* I/O buffer structure.
  211.    The `fname' field is nonzero for source files and #include files
  212.    and for the dummy text used for -D and -U.
  213.    It is zero for rescanning results of macro expansion
  214.    and for expanding macro arguments.  */
  215. #define INPUT_STACK_MAX 200
  216. struct file_buf {
  217.   char *fname;
  218.   int lineno;
  219.   int length;
  220.   U_CHAR *buf;
  221.   U_CHAR *bufp;
  222.   /* Macro that this level is the expansion of.
  223.      Included so that we can reenable the macro
  224.      at the end of this level.  */
  225.   struct hashnode *macro;
  226.   /* Value of if_stack at start of this file.
  227.      Used to prohibit unmatched #endif (etc) in an include file.  */
  228.   struct if_stack *if_stack;
  229.   /* Object to be freed at end of input at this level.  */
  230.   U_CHAR *free_ptr;
  231. } instack[INPUT_STACK_MAX];
  232.  
  233. /* Current nesting level of input sources.
  234.    `instack[indepth]' is the level currently being read.  */
  235. int indepth = -1;
  236. #define CHECK_DEPTH(code) \
  237.   if (indepth >= (INPUT_STACK_MAX - 1))                    \
  238.     {                                    \
  239.       error_with_line (line_for_error (instack[indepth].lineno),    \
  240.                "macro or #include recursion too deep");        \
  241.       code;                                \
  242.     }
  243.  
  244. /* Current depth in #include directives that use <...>.  */
  245. int system_include_depth = 0;
  246.  
  247. typedef struct file_buf FILE_BUF;
  248.  
  249. /* The output buffer.  Its LENGTH field is the amount of room allocated
  250.    for the buffer, not the number of chars actually present.  To get
  251.    that, subtract outbuf.buf from outbuf.bufp. */
  252.  
  253. #define OUTBUF_SIZE 10    /* initial size of output buffer */
  254. FILE_BUF outbuf;
  255.  
  256. /* Grow output buffer OBUF points at
  257.    so it can hold at least NEEDED more chars.  */
  258.  
  259. #define check_expand(OBUF, NEEDED)  \
  260.   (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED))   \
  261.    ? grow_outbuf ((OBUF), (NEEDED)) : 0)
  262.  
  263. struct file_name_list
  264.   {
  265.     struct file_name_list *next;
  266.     char *fname;
  267.   };
  268.  
  269. /* #include "file" looks in source file dir, then stack. */
  270. /* #include <file> just looks in the stack. */
  271. /* -I directories are added to the end, then the defaults are added. */
  272. #ifdef atarist
  273.  
  274. struct file_name_list include_canned_defaults[] =
  275.   {
  276.     { &include_canned_defaults[1], "\\gnu\\lib" },
  277.     { 0, "\\gnu\\include" }
  278.   };
  279.  
  280. struct file_name_list * include_defaults = &include_canned_defaults[0];
  281.  
  282. #else
  283.  
  284. struct file_name_list include_defaults[] =
  285.   {
  286. #ifndef VMS
  287. #ifdef CROSSATARI
  288.     { 0, CROSSINC }
  289. #else
  290. #ifdef atariminix
  291.     { 0, "/usr/local/gcc-include" }
  292. #else
  293.     { &include_defaults[1], GCC_INCLUDE_DIR },
  294.     { &include_defaults[2], "/usr/include" },
  295.     { 0, "/usr/local/include" }
  296. #endif
  297. #endif
  298. #else
  299.     { &include_defaults[1], "GNU_CC_INCLUDE:" },       /* GNU includes */
  300.     { &include_defaults[2], "SYS$SYSROOT:[SYSLIB.]" }, /* VAX-11 "C" includes */
  301.     { 0, "" },    /* This makes normal VMS filespecs work OK */
  302. #endif /* VMS */
  303.   };
  304. #endif /* atarist */
  305.  
  306. /* These are used instead of the above, for C++.  */
  307. #ifdef atarist
  308.  
  309. struct file_name_list include_canned_cplusplus_defaults[] =
  310.   {
  311.     { &include_canned_defaults[1], "\\gnu\\lib" },
  312.     { 0, "\\gnu\\include" }
  313.   };
  314.  
  315. struct file_name_list * cplusplus_include_defaults =
  316.                     &include_canned_cplusplus_defaults[0];
  317.  
  318. #else
  319.  
  320. #ifdef atariminix
  321. #define GPLUSPLUS_INCLUDE_DIR "/usr/local/g++-include"
  322. #define GCC_INCLUDE_DIR "/usr/local/gcc-include"
  323. #endif
  324.  
  325. struct file_name_list cplusplus_include_defaults[] =
  326.   {
  327. #ifndef VMS
  328.     /* Pick up GNU C++ specific include files.  */
  329.     { &cplusplus_include_defaults[1], GPLUSPLUS_INCLUDE_DIR },
  330.     /* Use GNU CC specific header files.  */
  331.     { &cplusplus_include_defaults[2], GCC_INCLUDE_DIR },
  332. #ifdef CROSSATARI
  333.     { 0, CROSSINC }
  334. #else
  335.     { 0, "/usr/include" }
  336. #endif
  337. #else
  338.     { &cplusplus_include_defaults[1], "GNU_GXX_INCLUDE:" },
  339.     { &cplusplus_include_defaults[2], "GNU_CC_INCLUDE:" },
  340.     /* VAX-11 C includes */
  341.     { &cplusplus_include_defaults[3], "SYS$SYSROOT:[SYSLIB.]" },
  342.     { 0, "" },    /* This makes normal VMS filespecs work OK */
  343. #endif /* VMS */
  344.   };
  345. #endif /* atarist */
  346.  
  347. struct file_name_list *include = 0;    /* First dir to search */
  348.     /* First dir to search for <file> */
  349. struct file_name_list *first_bracket_include = 0;
  350. struct file_name_list *last_include = 0;    /* Last in chain */
  351.  
  352. /* List of included files that contained #once.  */
  353. struct file_name_list *dont_repeat_files = 0;
  354.  
  355. /* List of other included files.  */
  356. struct file_name_list *all_include_files = 0;
  357.  
  358. /* Structure allocated for every #define.  For a simple replacement
  359.    such as
  360.        #define foo bar ,
  361.    nargs = -1, the `pattern' list is null, and the expansion is just
  362.    the replacement text.  Nargs = 0 means a functionlike macro with no args,
  363.    e.g.,
  364.        #define getchar() getc (stdin) .
  365.    When there are args, the expansion is the replacement text with the
  366.    args squashed out, and the reflist is a list describing how to
  367.    build the output from the input: e.g., "3 chars, then the 1st arg,
  368.    then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
  369.    The chars here come from the expansion.  Whatever is left of the
  370.    expansion after the last arg-occurrence is copied after that arg.
  371.    Note that the reflist can be arbitrarily long---
  372.    its length depends on the number of times the arguments appear in
  373.    the replacement text, not how many args there are.  Example:
  374.    #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
  375.    pattern list
  376.      { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
  377.    where (x, y) means (nchars, argno). */
  378.  
  379. typedef struct definition DEFINITION;
  380. struct definition {
  381.   int nargs;
  382.   int length;            /* length of expansion string */
  383.   U_CHAR *expansion;
  384.   struct reflist {
  385.     struct reflist *next;
  386.     char stringify;        /* nonzero if this arg was preceded by a
  387.                    # operator. */
  388.     char raw_before;        /* Nonzero if a ## operator before arg. */
  389.     char raw_after;        /* Nonzero if a ## operator after arg. */
  390.     int nchars;            /* Number of literal chars to copy before
  391.                    this arg occurrence.  */
  392.     int argno;            /* Number of arg to substitute (origin-0) */
  393.   } *pattern;
  394.   /* Names of macro args, concatenated in reverse order
  395.      with comma-space between them.
  396.      The only use of this is that we warn on redefinition
  397.      if this differs between the old and new definitions.  */
  398.   U_CHAR *argnames;
  399. };
  400.  
  401. /* different kinds of things that can appear in the value field
  402.    of a hash node.  Actually, this may be useless now. */
  403. union hashval {
  404.   int ival;
  405.   char *cpval;
  406.   DEFINITION *defn;
  407. };
  408.  
  409.  
  410. /* The structure of a node in the hash table.  The hash table
  411.    has entries for all tokens defined by #define commands (type T_MACRO),
  412.    plus some special tokens like __LINE__ (these each have their own
  413.    type, and the appropriate code is run when that type of node is seen.
  414.    It does not contain control words like "#define", which are recognized
  415.    by a separate piece of code. */
  416.  
  417. /* different flavors of hash nodes --- also used in keyword table */
  418. enum node_type {
  419.  T_DEFINE = 1,    /* the `#define' keyword */
  420.  T_INCLUDE,    /* the `#include' keyword */
  421.  T_IFDEF,    /* the `#ifdef' keyword */
  422.  T_IFNDEF,    /* the `#ifndef' keyword */
  423.  T_IF,        /* the `#if' keyword */
  424.  T_ELSE,    /* `#else' */
  425.  T_PRAGMA,    /* `#pragma' */
  426.  T_ELIF,    /* `#else' */
  427.  T_UNDEF,    /* `#undef' */
  428.  T_LINE,    /* `#line' */
  429.  T_ERROR,    /* `#error' */
  430.  T_ENDIF,    /* `#endif' */
  431.  T_SCCS,    /* `#sccs', used on system V.  */
  432.  T_IDENT,    /* `#ident', used on system V.  */
  433.  T_SPECLINE,    /* special symbol `__LINE__' */
  434.  T_DATE,    /* `__DATE__' */
  435.  T_FILE,    /* `__FILE__' */
  436.  T_BASE_FILE,    /* `__BASE_FILE__' */
  437.  T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
  438.  T_VERSION,    /* `__VERSION__' */
  439.  T_TIME,    /* `__TIME__' */
  440.  T_CONST,    /* Constant value, used by `__STDC__' */
  441.  T_MACRO,    /* macro defined by `#define' */
  442.  T_DISABLED,    /* macro temporarily turned off for rescan */
  443.  T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
  444.  T_UNUSED    /* Used for something not defined.  */
  445.  };
  446.  
  447. struct hashnode {
  448.   struct hashnode *next;    /* double links for easy deletion */
  449.   struct hashnode *prev;
  450.   struct hashnode **bucket_hdr;    /* also, a back pointer to this node's hash
  451.                    chain is kept, in case the node is the head
  452.                    of the chain and gets deleted. */
  453.   enum node_type type;        /* type of special token */
  454.   int length;            /* length of token, for quick comparison */
  455.   U_CHAR *name;            /* the actual name */
  456.   union hashval value;        /* pointer to expansion, or whatever */
  457. };
  458.  
  459. typedef struct hashnode HASHNODE;
  460.  
  461. /* Some definitions for the hash table.  The hash function MUST be
  462.    computed as shown in hashf () below.  That is because the rescan
  463.    loop computes the hash value `on the fly' for most tokens,
  464.    in order to avoid the overhead of a lot of procedure calls to
  465.    the hashf () function.  Hashf () only exists for the sake of
  466.    politeness, for use when speed isn't so important. */
  467.  
  468. #define HASHSIZE 1403
  469. HASHNODE *hashtab[HASHSIZE];
  470. #define HASHSTEP(old, c) ((old << 2) + c)
  471. #define MAKE_POS(v) (v & ~0x80000000) /* make number positive */
  472.  
  473. /* Symbols to predefine.  */
  474.  
  475. #ifdef CPP_PREDEFINES
  476. char *predefs = CPP_PREDEFINES;
  477. #else
  478. char *predefs = "";
  479. #endif
  480.  
  481. /* `struct directive' defines one #-directive, including how to handle it.  */
  482.  
  483. struct directive {
  484.   int length;            /* Length of name */
  485.   int (*func)();        /* Function to handle directive */
  486.   char *name;            /* Name of directive */
  487.   enum node_type type;        /* Code which describes which directive. */
  488.   char angle_brackets;        /* Nonzero => <...> is special.  */
  489.   char traditional_comments;    /* Nonzero: keep comments if -traditional.  */
  490.   char pass_thru;        /* Copy preprocessed directive to output file.  */
  491. };
  492.  
  493. /* Here is the actual list of #-directives, most-often-used first.  */
  494.  
  495. struct directive directive_table[] = {
  496.   {  6, do_define, "define", T_DEFINE, 0, 1},
  497.   {  2, do_if, "if", T_IF},
  498.   {  5, do_xifdef, "ifdef", T_IFDEF},
  499.   {  6, do_xifdef, "ifndef", T_IFNDEF},
  500.   {  5, do_endif, "endif", T_ENDIF},
  501.   {  4, do_else, "else", T_ELSE},
  502.   {  4, do_elif, "elif", T_ELIF},
  503.   {  4, do_line, "line", T_LINE},
  504.   {  7, do_include, "include", T_INCLUDE, 1},
  505.   {  5, do_undef, "undef", T_UNDEF},
  506.   {  5, do_error, "error", T_ERROR},
  507. #ifdef SCCS_DIRECTIVE
  508.   {  4, do_sccs, "sccs", T_SCCS},
  509. #endif
  510.   {  6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
  511.   {  -1, 0, "", T_UNUSED},
  512. };
  513.  
  514. /* table to tell if char can be part of a C identifier. */
  515. U_CHAR is_idchar[256];
  516. /* table to tell if char can be first char of a c identifier. */
  517. U_CHAR is_idstart[256];
  518. /* table to tell if c is horizontal space.  */
  519. U_CHAR is_hor_space[256];
  520. /* table to tell if c is horizontal or vertical space.  */
  521. U_CHAR is_space[256];
  522.  
  523. #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
  524. #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
  525.   
  526. int errors = 0;            /* Error counter for exit code */
  527.  
  528. /* Zero means dollar signs are punctuation.
  529.    -$ stores 0; -traditional, stores 1.  Default is 1 for VMS, 0 otherwise.
  530.    This must be 0 for correct processing of this ANSI C program:
  531.     #define foo(a) #a
  532.     #define lose(b) foo(b)
  533.     #define test$
  534.     lose(test)    */
  535. #ifndef DOLLARS_IN_IDENTIFIERS
  536. #define DOLLARS_IN_IDENTIFIERS 0
  537. #endif
  538. int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
  539.  
  540. FILE_BUF expand_to_temp_buffer ();
  541.  
  542. DEFINITION *collect_expansion ();
  543.  
  544. /* Stack of conditionals currently in progress
  545.    (including both successful and failing conditionals).  */
  546.  
  547. struct if_stack {
  548.   struct if_stack *next;    /* for chaining to the next stack frame */
  549.   char *fname;        /* copied from input when frame is made */
  550.   int lineno;            /* similarly */
  551.   int if_succeeded;        /* true if a leg of this if-group
  552.                     has been passed through rescan */
  553.   enum node_type type;        /* type of last directive seen in this group */
  554. };
  555. typedef struct if_stack IF_STACK_FRAME;
  556. IF_STACK_FRAME *if_stack = NULL;
  557.  
  558. /* Buffer of -M output.  */
  559.  
  560. char *deps_buffer;
  561.  
  562. /* Number of bytes allocated in above.  */
  563. int deps_allocated_size;
  564.  
  565. /* Number of bytes used.  */
  566. int deps_size;
  567.  
  568. /* Number of bytes since the last newline.  */
  569. int deps_column;
  570.  
  571. /* Nonzero means -I- has been seen,
  572.    so don't look for #include "foo" the source-file directory.  */
  573. int ignore_srcdir;
  574.  
  575. #ifdef atarist
  576. /* a little frobule to filter incoming file data */
  577. int eunuchs_read(f, buf, size)
  578. int f;
  579. char * buf;
  580. int size;
  581. {
  582.   char local_buf[1024];
  583.   register int result_size;
  584.   register char * local_bufp, * target_bufp;
  585.   register int buf_size, size_read;
  586.  
  587.   for (result_size = 0, target_bufp = buf ; size > 0 ; size -= 1024)
  588. /* do a buffer */
  589.       {
  590.     if (size > 1024)
  591.         buf_size = 1024;
  592.         else
  593.             buf_size = size;
  594.     size_read = read(f, &local_buf, buf_size);
  595.     for (local_bufp = (char * ) &local_buf; size_read > 0 ; size_read--)
  596.         if (*local_bufp == '\r')
  597.             local_bufp++;
  598.             else
  599.                 {
  600.             *target_bufp++ = *local_bufp++;
  601.             result_size++;
  602.             }
  603.     }
  604.   return(result_size);
  605. }
  606.  
  607. /* the following dingus is used in place of some calls to bcopy,
  608.    to ensure that backslashes get properly slashified when getting
  609.    shoved into strings.  Note that it returns the new pointer!!
  610.    There ought to be a better way... */
  611.  
  612. U_CHAR * slashifying_bcopy(from_buf, to_buf, nbytes)
  613. U_CHAR * from_buf, * to_buf;
  614. int nbytes;
  615. {
  616.   for ( ; nbytes > 0 ; )
  617.     {
  618.     if(*from_buf == '\\')
  619.         *to_buf++ = '\\';
  620.     *to_buf++ = *from_buf++;
  621.     nbytes--;
  622.     }
  623.   return(to_buf);
  624. }
  625.  
  626. #endif        /* atarist */
  627.  
  628. #ifndef atarist
  629. /* Handler for SIGPIPE.  */
  630.  
  631. static void
  632. pipe_closed ()
  633. {
  634.   fatal ("output pipe has been closed");
  635. }
  636. #endif
  637.  
  638. int
  639. main (argc, argv)
  640.      int argc;
  641.      char **argv;
  642. {
  643.   int st_mode;
  644.   long st_size;
  645.   char *in_fname, *out_fname;
  646.   int f, i;
  647.   FILE_BUF *fp;
  648.   char **pend_files = (char **) xmalloc (argc * sizeof (char *));
  649.   char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
  650.   char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
  651.   int inhibit_predefs = 0;
  652.   int no_standard_includes = 0;
  653.  
  654.   /* Non-0 means don't output the preprocessed program.  */
  655.   int inhibit_output = 0;
  656.  
  657.   /* Stream on which to print the dependency information.  */
  658.   FILE *deps_stream = 0;
  659.   /* Target-name to write with the dependency information.  */
  660.   char *deps_target = 0;
  661.  
  662. #ifdef atarist
  663. /* turn this on if you are going to set the TOS 1.4 dont clear heap flag */
  664. /*  _malloczero(1); */     /* zero mallocs by default */
  665. #endif  
  666.  
  667. #ifdef RLIMIT_STACK
  668.   /* Get rid of any avoidable limit on stack size.  */
  669.   {
  670.     struct rlimit rlim;
  671.  
  672.     /* Set the stack limit huge so that alloca (particularly stringtab
  673.      * in dbxread.c) does not fail. */
  674.     getrlimit (RLIMIT_STACK, &rlim);
  675.     rlim.rlim_cur = rlim.rlim_max;
  676.     setrlimit (RLIMIT_STACK, &rlim);
  677.   }
  678. #endif /* RLIMIT_STACK defined */
  679.  
  680.   progname = argv[0];
  681. #ifdef VMS
  682.   {
  683.     /* Remove directories from PROGNAME.  */
  684.     char *s;
  685.     extern char *rindex ();
  686.  
  687.     progname = savestring (argv[0]);
  688.  
  689.     if (!(s = rindex (progname, ']')))
  690.       s = rindex (progname, ':');
  691.     if (s)
  692.       strcpy (progname, s+1);
  693.     if (s = rindex (progname, '.'))
  694.       *s = '\0';
  695.   }
  696. #endif
  697.  
  698.   in_fname = NULL;
  699.   out_fname = NULL;
  700.  
  701.   /* Initialize is_idchar to allow $.  */
  702.   dollars_in_ident = 1;
  703.   initialize_char_syntax ();
  704.   dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
  705.  
  706.   no_line_commands = 0;
  707.   no_trigraphs = 1;
  708.   dump_macros = 0;
  709.   no_output = 0;
  710.   cplusplus = 0;
  711. #ifdef CPLUSPLUS
  712.   cplusplus = 1;
  713. #endif
  714.  
  715. #ifndef atarist
  716.   signal (SIGPIPE, pipe_closed);
  717. #endif  
  718.  
  719. #ifndef VMS
  720.   max_include_len
  721. #ifdef atarist
  722.     =    sizeof ("/usr/include/CC");
  723. #else
  724.     = max (max (sizeof (GCC_INCLUDE_DIR),
  725.         sizeof (GPLUSPLUS_INCLUDE_DIR)),
  726.        sizeof ("/usr/include/CC"));
  727. #endif
  728. #else /* VMS */
  729.   max_include_len
  730.     = sizeof("SYS$SYSROOT:[SYSLIB.]");
  731. #endif /* VMS */
  732.  
  733.   bzero (pend_files, argc * sizeof (char *));
  734.   bzero (pend_defs, argc * sizeof (char *));
  735.   bzero (pend_undefs, argc * sizeof (char *));
  736.  
  737. #ifdef atarist
  738. /* see if we have an indication of where the GNU library directory is */
  739.   {
  740.     char * gnulib = (char * ) getenv("GNUINC");
  741.     struct file_name_list * gnulib_dir;
  742.  
  743.     if(!gnulib) gnulib = (char *) getenv("GNULIB"); /* try GNULIB */
  744.     if (gnulib)
  745.     {
  746.     gnulib_dir = (struct file_name_list * ) 
  747.         xmalloc(sizeof(struct file_name_list));
  748.     gnulib_dir->fname = xmalloc(strlen(gnulib) + 1);
  749.     strcpy(gnulib_dir->fname, gnulib);
  750. /* cons it onto the front of the defaults. */
  751.     gnulib_dir->next = include_defaults;
  752.     include_defaults = gnulib_dir;
  753.     }
  754.   }
  755. #endif
  756.  
  757.   /* Process switches and find input file name.  */
  758.  
  759.   for (i = 1; i < argc; i++) {
  760.     if (argv[i][0] != '-') {
  761.       if (out_fname != NULL)
  762.     fatal ("Usage: %s [switches] input output", argv[0]);
  763.       else if (in_fname != NULL)
  764.     out_fname = argv[i];
  765.       else
  766.     in_fname = argv[i];
  767.     } else {
  768.       switch (argv[i][1]) {
  769.  
  770.       case 'i':
  771.     if (argv[i][2] != 0)
  772.       pend_files[i] = argv[i] + 2;
  773.     else if (i + 1 == argc)
  774.       fatal ("Filename missing after -i option");
  775.     else
  776.       pend_files[i] = argv[i+1], i++;
  777.     break;
  778.  
  779.       case 'o':
  780.     if (out_fname != NULL)
  781.       fatal ("Output filename specified twice");
  782.     if (i + 1 == argc)
  783.       fatal ("Filename missing after -o option");
  784.     out_fname = argv[++i];
  785.     if (!strcmp (out_fname, "-"))
  786.       out_fname = "";
  787.     break;
  788.  
  789.       case 'p':
  790.     pedantic = 1;
  791.     break;
  792.  
  793.       case 't':
  794.     if (!strcmp (argv[i], "-traditional")) {
  795.       traditional = 1;
  796.       dollars_in_ident = 1;
  797.     } else if (!strcmp (argv[i], "-trigraphs")) {
  798.       no_trigraphs = 0;
  799.     }
  800.     break;
  801.  
  802.       case '+':
  803.     cplusplus = 1;
  804.     break;
  805.  
  806.       case 'W':
  807.     if (!strcmp (argv[i], "-Wtrigraphs")) {
  808.       warn_trigraphs = 1;
  809.     }
  810.     if (!strcmp (argv[i], "-Wcomments"))
  811.       warn_comments = 1;
  812.     if (!strcmp (argv[i], "-Wcomment"))
  813.       warn_comments = 1;
  814.     if (!strcmp (argv[i], "-Wall")) {
  815.       warn_trigraphs = 1;
  816.       warn_comments = 1;
  817.     }
  818.     break;
  819.  
  820.       case 'M':
  821.     if (!strcmp (argv[i], "-M"))
  822.       print_deps = 2;
  823.     else if (!strcmp (argv[i], "-MM"))
  824.       print_deps = 1;
  825.     inhibit_output = 1;
  826.     break;
  827.  
  828.       case 'd':
  829.     dump_macros = 1;
  830.     no_output = 1;
  831.     break;
  832.  
  833.       case 'v':
  834.     fprintf (stderr, "GNU CPP version %s\n", version_string);
  835.     break;
  836.  
  837.       case 'D':
  838.     {
  839.       char *p, *p1;
  840.  
  841.       if (argv[i][2] != 0)
  842.         p = argv[i] + 2;
  843.       else if (i + 1 == argc)
  844.         fatal ("Macro name missing after -D option");
  845.       else
  846.         p = argv[++i];
  847.  
  848.       if ((p1 = (char *) index (p, '=')) != NULL)
  849.         *p1 = ' ';
  850.       pend_defs[i] = p;
  851.     }
  852.     break;
  853.  
  854.       case 'U':        /* JF #undef something */
  855.     if (argv[i][2] != 0)
  856.       pend_undefs[i] = argv[i] + 2;
  857.     else if (i + 1 == argc)
  858.       fatal ("Macro name missing after -U option");
  859.     else
  860.       pend_undefs[i] = argv[i+1], i++;
  861.     break;
  862.  
  863.       case 'C':
  864.     put_out_comments = 1;
  865.     break;
  866.  
  867.       case 'E':            /* -E comes from cc -E; ignore it.  */
  868.     break;
  869.  
  870.       case 'P':
  871.     no_line_commands = 1;
  872.     break;
  873.  
  874.       case '$':            /* Don't include $ in identifiers.  */
  875.     dollars_in_ident = 0;
  876.     break;
  877.  
  878.       case 'I':            /* Add directory to path for includes.  */
  879.     {
  880.       struct file_name_list *dirtmp;
  881.  
  882.       if (! ignore_srcdir && !strcmp (argv[i] + 2, "-"))
  883.         ignore_srcdir = 1;
  884.       else {
  885.         dirtmp = (struct file_name_list *)
  886.           xmalloc (sizeof (struct file_name_list));
  887.         dirtmp->next = 0;        /* New one goes on the end */
  888.         if (include == 0)
  889.           include = dirtmp;
  890.         else
  891.           last_include->next = dirtmp;
  892.         last_include = dirtmp;    /* Tail follows the last one */
  893.         if (argv[i][2] != 0)
  894.           dirtmp->fname = argv[i] + 2;
  895.         else if (i + 1 == argc)
  896.           fatal ("Directory name missing after -I option");
  897.         else
  898.           dirtmp->fname = argv[++i];
  899.         if (strlen (dirtmp->fname) > max_include_len)
  900.           max_include_len = strlen (dirtmp->fname);
  901.         if (ignore_srcdir && first_bracket_include == 0)
  902.           first_bracket_include = dirtmp;
  903.         }
  904.     }
  905.     break;
  906.  
  907.       case 'n':
  908.     /* -nostdinc causes no default include directories.
  909.        You must specify all include-file directories with -I.  */
  910.     no_standard_includes = 1;
  911.     break;
  912.  
  913.       case 'u':
  914.     /* Sun compiler passes undocumented switch "-undef".
  915.        Let's assume it means to inhibit the predefined symbols.  */
  916.     inhibit_predefs = 1;
  917.     break;
  918.  
  919.       case '\0': /* JF handle '-' as file name meaning stdin or stdout */
  920.     if (in_fname == NULL) {
  921.       in_fname = "";
  922.       break;
  923.     } else if (out_fname == NULL) {
  924.       out_fname = "";
  925.       break;
  926.     }    /* else fall through into error */
  927.  
  928.       default:
  929.     fatal ("Invalid option `%s'", argv[i]);
  930.       }
  931.     }
  932.   }
  933.  
  934.   /* Now that dollars_in_ident is known, initialize is_idchar.  */
  935.   initialize_char_syntax ();
  936.  
  937.   /* Install __LINE__, etc.  Must follow initialize_char_syntax
  938.      and option processing.  */
  939.   initialize_builtins ();
  940.  
  941.   /* Do standard #defines that identify processor type.  */
  942.  
  943.   if (!inhibit_predefs) {
  944.     char *p = (char *) alloca (strlen (predefs) + 1);
  945.     strcpy (p, predefs);
  946.     while (*p) {
  947.       char *q;
  948.       if (p[0] != '-' || p[1] != 'D')
  949.     abort ();
  950.       q = &p[2];
  951.       while (*p && *p != ' ') p++;
  952.       if (*p != 0)
  953.     *p++= 0;
  954.       make_definition (q);
  955.     }
  956.   }
  957.  
  958.   /* Do defines specified with -D.  */
  959.   for (i = 1; i < argc; i++)
  960.     if (pend_defs[i])
  961.       make_definition (pend_defs[i]);
  962.  
  963.   /* Do undefines specified with -U.  */
  964.   for (i = 1; i < argc; i++)
  965.     if (pend_undefs[i])
  966.       make_undef (pend_undefs[i]);
  967.  
  968.   /* Unless -fnostdinc,
  969.      tack on the standard include file dirs to the specified list */
  970.   if (!no_standard_includes) {
  971.     if (include == 0)
  972.       include = (cplusplus ? cplusplus_include_defaults : include_defaults);
  973.     else
  974.       last_include->next
  975.     = (cplusplus ? cplusplus_include_defaults : include_defaults);
  976.     /* Make sure the list for #include <...> also has the standard dirs.  */
  977.     if (ignore_srcdir && first_bracket_include == 0)
  978.       first_bracket_include
  979.     = (cplusplus ? cplusplus_include_defaults : include_defaults);
  980.   }
  981.  
  982.   /* Initialize output buffer */
  983.  
  984.   outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
  985.   outbuf.bufp = outbuf.buf;
  986.   outbuf.length = OUTBUF_SIZE;
  987.  
  988.   /* Scan the -i files before the main input.
  989.      Much like #including them, but with no_output set
  990.      so that only their macro definitions matter.  */
  991.  
  992.   no_output++;
  993.   for (i = 1; i < argc; i++)
  994.     if (pend_files[i]) {
  995.       int fd = open (pend_files[i], O_RDONLY, 0666);
  996.       if (fd < 0) {
  997.     perror_with_name (pend_files[i]);
  998.     return FATAL_EXIT_CODE;
  999.       }
  1000.       finclude (fd, pend_files[i], &outbuf);
  1001.     }
  1002.   no_output--;
  1003.  
  1004.   /* Create an input stack level for the main input file
  1005.      and copy the entire contents of the file into it.  */
  1006.  
  1007.   fp = &instack[++indepth];
  1008.  
  1009.   /* JF check for stdin */
  1010.   if (in_fname == NULL || *in_fname == 0) {
  1011.     in_fname = "";
  1012.     f = 0;
  1013.   } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
  1014.     goto perror;
  1015.  
  1016.   /* Either of two environment variables can specify output of deps.
  1017.      Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
  1018.      where OUTPUT_FILE is the file to write deps info to
  1019.      and DEPS_TARGET is the target to mention in the deps.  */
  1020.  
  1021.   if (print_deps == 0
  1022.       && (getenv ("SUNPRO_DEPENDENCIES") != 0
  1023.       || getenv ("DEPENDENCIES_OUTPUT") != 0))
  1024.     {
  1025.       char *spec = getenv ("DEPENDENCIES_OUTPUT");
  1026.       char *s;
  1027.       char *output_file;
  1028.  
  1029.       if (spec == 0)
  1030.     {
  1031.       spec = getenv ("SUNPRO_DEPENDENCIES");
  1032.       print_deps = 2;
  1033.     }
  1034.       else
  1035.     print_deps = 1;
  1036.  
  1037.       s = spec;
  1038.       /* Find the space before the DEPS_TARGET, if there is one.  */
  1039.       /* Don't use `index'; that causes trouble on USG.  */
  1040.       while (*s != 0 && *s != ' ') s++;
  1041.       if (*s != 0)
  1042.     {
  1043.       deps_target = s + 1;
  1044.       output_file = (char *) xmalloc (s - spec + 1);
  1045.       bcopy (spec, output_file, s - spec);
  1046.       output_file[s - spec] = 0;
  1047.     }
  1048.       else
  1049.     {
  1050.       deps_target = 0;
  1051.       output_file = spec;
  1052.     }
  1053.       
  1054.       deps_stream = fopen (output_file, "a");
  1055.       if (deps_stream == 0)
  1056.     pfatal_with_name (output_file);
  1057.     }
  1058.   /* If the -M option was used, output the deps to standard output.  */
  1059.   else if (print_deps)
  1060.     deps_stream = stdout;
  1061.  
  1062.   /* For -M, print the expected object file name
  1063.      as the target of this Make-rule.  */
  1064.   if (print_deps) {
  1065.     deps_allocated_size = 200;
  1066.     deps_buffer = (char *) xmalloc (deps_allocated_size);
  1067.     deps_buffer[0] = 0;
  1068.     deps_size = 0;
  1069.     deps_column = 0;
  1070.  
  1071.     if (deps_target) {
  1072.       deps_output (deps_target, 0);
  1073.       deps_output (":", 0);
  1074.     } else if (*in_fname == 0)
  1075.       deps_output ("-: ", 0);
  1076.     else {
  1077.       int len;
  1078.       char *p = in_fname;
  1079.       char *p1 = p;
  1080.       /* Discard all directory prefixes from P.  */
  1081.       while (*p1) {
  1082.     if (*p1 == '/')
  1083.       p = p1 + 1;
  1084.     p1++;
  1085.       }
  1086.       /* Output P, but remove known suffixes.  */
  1087.       len = strlen (p);
  1088.       if (p[len - 2] == '.' && p[len - 1] == 'c')
  1089.     deps_output (p, len - 2);
  1090.       else if (p[len - 3] == '.'
  1091.            && p[len - 2] == 'c'
  1092.            && p[len - 1] == 'c')
  1093.     deps_output (p, len - 3);
  1094.       else
  1095.     deps_output (p, 0);
  1096.       /* Supply our own suffix.  */
  1097.       deps_output (".o : ", 0);
  1098.       deps_output (in_fname, 0);
  1099.       deps_output (" ", 0);
  1100.     }
  1101.   }
  1102.  
  1103.   file_size_and_mode (f, &st_mode, &st_size);
  1104.   fp->fname = in_fname;
  1105.   fp->lineno = 1;
  1106.   /* JF all this is mine about reading pipes and ttys */
  1107.   if ((st_mode & S_IFMT) != S_IFREG) {
  1108.     /* Read input from a file that is not a normal disk file.
  1109.        We cannot preallocate a buffer with the correct size,
  1110.        so we must read in the file a piece at the time and make it bigger.  */
  1111.     int size;
  1112.     int bsize;
  1113.     int cnt;
  1114.     U_CHAR *bufp;
  1115.  
  1116.     bsize = 2000;
  1117.     size = 0;
  1118.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  1119.     bufp = fp->buf;
  1120.     for (;;) {
  1121. #ifdef atarist
  1122.       cnt = eunuchs_read (f, bufp, bsize - size);
  1123. #else
  1124.       cnt = read (f, bufp, bsize - size);
  1125. #endif
  1126.       if (cnt < 0) goto perror;    /* error! */
  1127.       if (cnt == 0) break;    /* End of file */
  1128.       size += cnt;
  1129.       bufp += cnt;
  1130.       if (bsize == size) {    /* Buffer is full! */
  1131.         bsize *= 2;
  1132.         fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  1133.     bufp = fp->buf + size;    /* May have moved */
  1134.       }
  1135.     }
  1136.     fp->length = size;
  1137.   } else {
  1138.     /* Read a file whose size we can determine in advance.
  1139.        For the sake of VMS, st_size is just an upper bound.  */
  1140.     long i;
  1141.     fp->length = 0;
  1142.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  1143.  
  1144.     while (st_size > 0) {
  1145. #ifdef atarist
  1146.       i = eunuchs_read (f, fp->buf + fp->length, st_size);
  1147. #else
  1148.       i = read (f, fp->buf + fp->length, st_size);
  1149. #endif
  1150.       if (i <= 0) {
  1151.         if (i == 0) break;
  1152.     goto perror;
  1153.       }
  1154.       fp->length += i;
  1155.       st_size -= i;
  1156.     }
  1157.   }
  1158.   fp->bufp = fp->buf;
  1159.   fp->if_stack = if_stack;
  1160.   
  1161.   /* Unless inhibited, convert trigraphs in the input.  */
  1162.  
  1163.   if (!no_trigraphs)
  1164.     trigraph_pcp (fp);
  1165.  
  1166.   /* Make sure data ends with a newline.  And put a null after it.  */
  1167.  
  1168.   if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
  1169.     fp->buf[fp->length++] = '\n';
  1170.   fp->buf[fp->length] = '\0';
  1171.  
  1172.   /* Now that we know the input file is valid, open the output.  */
  1173.  
  1174.   if (!out_fname || !strcmp (out_fname, ""))
  1175.     out_fname = "stdout";
  1176.   else if (! freopen (out_fname, "w", stdout))
  1177.     pfatal_with_name (out_fname);
  1178.  
  1179.   output_line_command (fp, &outbuf, 0, same_file);
  1180.  
  1181.   /* Scan the input, processing macros and directives.  */
  1182.  
  1183.   rescan (&outbuf, 0);
  1184.  
  1185.   /* Now we have processed the entire input
  1186.      Write whichever kind of output has been requested.  */
  1187.  
  1188.  
  1189.   if (dump_macros)
  1190.     dump_all_macros ();
  1191.   else if (! inhibit_output && deps_stream != stdout) {
  1192.     if (write (fileno (stdout), outbuf.buf, outbuf.bufp - outbuf.buf) < 0)
  1193.       fatal ("I/O error on output");
  1194.   }
  1195.  
  1196.   if (print_deps) {
  1197.     fputs (deps_buffer, deps_stream);
  1198.     putc ('\n', deps_stream);
  1199.     if (deps_stream != stdout) {
  1200.       fclose (deps_stream);
  1201.       if (ferror (deps_stream))
  1202.     fatal ("I/O error on output");
  1203.     }
  1204.   }
  1205.  
  1206.   if (ferror (stdout))
  1207.     fatal ("I/O error on output");
  1208.  
  1209. #ifdef atarist
  1210.   close(fileno(stdout));        /* this shouldn't be necessary */
  1211. #endif
  1212.   if (errors)
  1213.     exit (FATAL_EXIT_CODE);
  1214.   exit (SUCCESS_EXIT_CODE);
  1215.  
  1216.  perror:
  1217.   pfatal_with_name (in_fname);
  1218. }
  1219.  
  1220. /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
  1221.    before main CCCP processing.  Name `pcp' is also in honor of the
  1222.    drugs the trigraph designers must have been on.
  1223.  
  1224.    Using an extra pass through the buffer takes a little extra time,
  1225.    but is infinitely less hairy than trying to handle ??/" inside
  1226.    strings, etc. everywhere, and also makes sure that trigraphs are
  1227.    only translated in the top level of processing. */
  1228.  
  1229. trigraph_pcp (buf)
  1230.      FILE_BUF *buf;
  1231. {
  1232.   register U_CHAR c, *fptr, *bptr, *sptr;
  1233.   int len;
  1234.  
  1235.   fptr = bptr = sptr = buf->buf;
  1236.   while ((sptr = (U_CHAR *) index (sptr, '?')) != NULL) {
  1237.     if (*++sptr != '?')
  1238.       continue;
  1239.     switch (*++sptr) {
  1240.       case '=':
  1241.       c = '#';
  1242.       break;
  1243.     case '(':
  1244.       c = '[';
  1245.       break;
  1246.     case '/':
  1247.       c = '\\';
  1248.       break;
  1249.     case ')':
  1250.       c = ']';
  1251.       break;
  1252.     case '\'':
  1253.       c = '^';
  1254.       break;
  1255.     case '<':
  1256.       c = '{';
  1257.       break;
  1258.     case '!':
  1259.       c = '|';
  1260.       break;
  1261.     case '>':
  1262.       c = '}';
  1263.       break;
  1264.     case '-':
  1265.       c  = '~';
  1266.       break;
  1267.     case '?':
  1268.       sptr--;
  1269.       continue;
  1270.     default:
  1271.       continue;
  1272.     }
  1273.     len = sptr - fptr - 2;
  1274.     if (bptr != fptr && len > 0)
  1275.       bcopy (fptr, bptr, len);    /* BSD doc says bcopy () works right
  1276.                    for overlapping strings.  In ANSI
  1277.                    C, this will be memmove (). */
  1278.     bptr += len;
  1279.     *bptr++ = c;
  1280.     fptr = ++sptr;
  1281.   }
  1282.   len = buf->length - (fptr - buf->buf);
  1283.   if (bptr != fptr && len > 0)
  1284.     bcopy (fptr, bptr, len);
  1285.   buf->length -= fptr - bptr;
  1286.   buf->buf[buf->length] = '\0';
  1287.   if (warn_trigraphs && fptr != bptr)
  1288.     warning ("%d trigraph(s) encountered", (fptr - bptr) / 2);
  1289. }
  1290.  
  1291. /* Move all backslash-newline pairs out of embarrassing places.
  1292.    Exchange all such pairs following BP
  1293.    with any potentially-embarrasing characters that follow them.
  1294.    Potentially-embarrassing characters are / and *
  1295.    (because a backslash-newline inside a comment delimiter
  1296.    would cause it not to be recognized).  */
  1297.  
  1298. newline_fix (bp)
  1299.      U_CHAR *bp;
  1300. {
  1301.   register U_CHAR *p = bp;
  1302.   register int count = 0;
  1303.  
  1304.   /* First count the backslash-newline pairs here.  */
  1305.  
  1306.   while (*p++ == '\\' && *p++ == '\n')
  1307.     count++;
  1308.  
  1309.   p = bp + count * 2;
  1310.  
  1311.   /* What follows the backslash-newlines is not embarrassing.  */
  1312.  
  1313.   if (count == 0 || (*p != '/' && *p != '*'))
  1314.     return;
  1315.  
  1316.   /* Copy all potentially embarrassing characters
  1317.      that follow the backslash-newline pairs
  1318.      down to where the pairs originally started.  */
  1319.  
  1320.   while (*p == '*' || *p == '/')
  1321.     *bp++ = *p++;
  1322.  
  1323.   /* Now write the same number of pairs after the embarrassing chars.  */
  1324.   while (count-- > 0) {
  1325.     *bp++ = '\\';
  1326.     *bp++ = '\n';
  1327.   }
  1328. }
  1329.  
  1330. /* Like newline_fix but for use within a directive-name.
  1331.    Move any backslash-newlines up past any following symbol constituents.  */
  1332.  
  1333. name_newline_fix (bp)
  1334.      U_CHAR *bp;
  1335. {
  1336.   register U_CHAR *p = bp;
  1337.   register int count = 0;
  1338.  
  1339.   /* First count the backslash-newline pairs here.  */
  1340.  
  1341.   while (*p++ == '\\' && *p++ == '\n')
  1342.     count++;
  1343.  
  1344.   p = bp + count * 2;
  1345.  
  1346.   /* What follows the backslash-newlines is not embarrassing.  */
  1347.  
  1348.   if (count == 0 || !is_idchar[*p])
  1349.     return;
  1350.  
  1351.   /* Copy all potentially embarrassing characters
  1352.      that follow the backslash-newline pairs
  1353.      down to where the pairs originally started.  */
  1354.  
  1355.   while (is_idchar[*p])
  1356.     *bp++ = *p++;
  1357.  
  1358.   /* Now write the same number of pairs after the embarrassing chars.  */
  1359.   while (count-- > 0) {
  1360.     *bp++ = '\\';
  1361.     *bp++ = '\n';
  1362.   }
  1363. }
  1364.  
  1365. /*
  1366.  * The main loop of the program.
  1367.  *
  1368.  * Read characters from the input stack, transferring them to the
  1369.  * output buffer OP.
  1370.  *
  1371.  * Macros are expanded and push levels on the input stack.
  1372.  * At the end of such a level it is popped off and we keep reading.
  1373.  * At the end of any other kind of level, we return.
  1374.  * #-directives are handled, except within macros.
  1375.  *
  1376.  * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
  1377.  * and insert them when appropriate.  This is set while scanning macro
  1378.  * arguments before substitution.  It is zero when scanning for final output.
  1379.  *   There are three types of Newline markers:
  1380.  *   * Newline -  follows a macro name that was not expanded
  1381.  *     because it appeared inside an expansion of the same macro.
  1382.  *     This marker prevents future expansion of that identifier.
  1383.  *     When the input is rescanned into the final output, these are deleted.
  1384.  *     These are also deleted by ## concatenation.
  1385.  *   * Newline Space (or Newline and any other whitespace character)
  1386.  *     stands for a place that tokens must be separated or whitespace
  1387.  *     is otherwise desirable, but where the ANSI standard specifies there
  1388.  *     is no whitespace.  This marker turns into a Space (or whichever other
  1389.  *     whitespace char appears in the marker) in the final output,
  1390.  *     but it turns into nothing in an argument that is stringified with #.
  1391.  *     Such stringified arguments are the only place where the ANSI standard
  1392.  *     specifies with precision that whitespace may not appear.
  1393.  *
  1394.  * During this function, IP->bufp is kept cached in IBP for speed of access.
  1395.  * Likewise, OP->bufp is kept in OBP.  Before calling a subroutine
  1396.  * IBP, IP and OBP must be copied back to memory.  IP and IBP are
  1397.  * copied back with the RECACHE macro.  OBP must be copied back from OP->bufp
  1398.  * explicitly, and before RECACHE, since RECACHE uses OBP.
  1399.  */
  1400.  
  1401. rescan (op, output_marks)
  1402.      FILE_BUF *op;
  1403.      int output_marks;
  1404. {
  1405.   /* Character being scanned in main loop.  */
  1406.   register U_CHAR c;
  1407.  
  1408.   /* Length of pending accumulated identifier.  */
  1409.   register int ident_length = 0;
  1410.  
  1411.   /* Hash code of pending accumulated identifier.  */
  1412.   register int hash = 0;
  1413.  
  1414.   /* Current input level (&instack[indepth]).  */
  1415.   FILE_BUF *ip;
  1416.  
  1417.   /* Pointer for scanning input.  */
  1418.   register U_CHAR *ibp;
  1419.  
  1420.   /* Pointer to end of input.  End of scan is controlled by LIMIT.  */
  1421.   register U_CHAR *limit;
  1422.  
  1423.   /* Pointer for storing output.  */
  1424.   register U_CHAR *obp;
  1425.  
  1426.   /* REDO_CHAR is nonzero if we are processing an identifier
  1427.      after backing up over the terminating character.
  1428.      Sometimes we process an identifier without backing up over
  1429.      the terminating character, if the terminating character
  1430.      is not special.  Backing up is done so that the terminating character
  1431.      will be dispatched on again once the identifier is dealt with.  */
  1432.   int redo_char = 0;
  1433.  
  1434.   /* 1 if within an identifier inside of which a concatenation
  1435.      marker (Newline -) has been seen.  */
  1436.   int concatenated = 0;
  1437.  
  1438.   /* While scanning a comment or a string constant,
  1439.      this records the line it started on, for error messages.  */
  1440.   int start_line;
  1441.  
  1442.   /* Record position of last `real' newline.  */
  1443.   U_CHAR *beg_of_line;
  1444.  
  1445. /* Pop the innermost input stack level, assuming it is a macro expansion.  */
  1446.  
  1447. #define POPMACRO \
  1448. do { ip->macro->type = T_MACRO;        \
  1449.      if (ip->free_ptr) free (ip->free_ptr);    \
  1450.      --indepth; } while (0)
  1451.  
  1452. /* Reload `rescan's local variables that describe the current
  1453.    level of the input stack.  */
  1454.  
  1455. #define RECACHE  \
  1456. do { ip = &instack[indepth];        \
  1457.      ibp = ip->bufp;            \
  1458.      limit = ip->buf + ip->length;    \
  1459.      op->bufp = obp;            \
  1460.      check_expand (op, limit - ibp);    \
  1461.      beg_of_line = 0;            \
  1462.      obp = op->bufp; } while (0)
  1463.  
  1464.   if (no_output && instack[indepth].fname != 0)
  1465.     skip_if_group (&instack[indepth], 1);
  1466.  
  1467.   obp = op->bufp;
  1468.   RECACHE;
  1469.   beg_of_line = ibp;
  1470.  
  1471.   /* Our caller must always put a null after the end of
  1472.      the input at each input stack level.  */
  1473.   if (*limit != 0)
  1474.     abort ();
  1475.  
  1476.   while (1) {
  1477.     c = *ibp++;
  1478.     *obp++ = c;
  1479.  
  1480.     switch (c) {
  1481.     case '\\':
  1482.       if (ibp >= limit)
  1483.     break;
  1484.       if (*ibp == '\n') {
  1485.     /* Always merge lines ending with backslash-newline,
  1486.        even in middle of identifier.  */
  1487.     ++ibp;
  1488.     ++ip->lineno;
  1489.     --obp;        /* remove backslash from obuf */
  1490.     break;
  1491.       }
  1492.       /* Otherwise, backslash suppresses specialness of following char,
  1493.      so copy it here to prevent the switch from seeing it.
  1494.      But first get any pending identifier processed.  */
  1495.       if (ident_length > 0)
  1496.     goto specialchar;
  1497.       *obp++ = *ibp++;
  1498.       break;
  1499.  
  1500.     case '#':
  1501.       /* If this is expanding a macro definition, don't recognize
  1502.      preprocessor directives.  */
  1503.       if (ip->macro != 0)
  1504.     goto randomchar;
  1505.       if (ident_length)
  1506.     goto specialchar;
  1507.  
  1508.       /* # keyword: a # must be first nonblank char on the line */
  1509.       if (beg_of_line == 0)
  1510.     goto randomchar;
  1511.       {
  1512.     U_CHAR *bp;
  1513.  
  1514.     /* Scan from start of line, skipping whitespace, comments
  1515.        and backslash-newlines, and see if we reach this #.
  1516.        If not, this # is not special.  */
  1517.     bp = beg_of_line;
  1518.     while (1) {
  1519.       if (is_hor_space[*bp])
  1520.         bp++;
  1521.       else if (*bp == '\\' && bp[1] == '\n')
  1522.         bp += 2;
  1523.       else if (*bp == '/' && bp[1] == '*') {
  1524.         bp += 2;
  1525.         while (!(*bp == '*' && bp[1] == '/'))
  1526.           bp++;
  1527.         bp += 2;
  1528.       }
  1529.       else if (cplusplus && *bp == '/' && bp[1] == '/') {
  1530.         bp += 2;
  1531.         while (*bp++ != '\n') ;
  1532.       }
  1533.       else break;
  1534.     }
  1535.     if (bp + 1 != ibp)
  1536.       goto randomchar;
  1537.       }
  1538.  
  1539.       /* This # can start a directive.  */
  1540.  
  1541.       --obp;        /* Don't copy the '#' */
  1542.  
  1543.       ip->bufp = ibp;
  1544.       op->bufp = obp;
  1545.       if (! handle_directive (ip, op)) {
  1546. #ifdef USE_C_ALLOCA
  1547.     alloca (0);
  1548. #endif
  1549.     /* Not a known directive: treat it as ordinary text.
  1550.        IP, OP, IBP, etc. have not been changed.  */
  1551.     if (no_output && instack[indepth].fname) {
  1552.       /* If not generating expanded output,
  1553.          what we do with ordinary text is skip it.
  1554.          Discard everything until next # directive.  */
  1555.       skip_if_group (&instack[indepth], 1);
  1556.       RECACHE;
  1557.       beg_of_line = ibp;
  1558.       break;
  1559.     }
  1560.     ++obp;        /* Copy the '#' after all */
  1561.     goto randomchar;
  1562.       }
  1563. #ifdef USE_C_ALLOCA
  1564.       alloca (0);
  1565. #endif
  1566.       /* A # directive has been successfully processed.  */
  1567.       /* If not generating expanded output, ignore everything until
  1568.      next # directive.  */
  1569.       if (no_output && instack[indepth].fname)
  1570.     skip_if_group (&instack[indepth], 1);
  1571.       obp = op->bufp;
  1572.       RECACHE;
  1573.       beg_of_line = ibp;
  1574.       break;
  1575.  
  1576.     case '\"':            /* skip quoted string */
  1577.     case '\'':
  1578.       /* A single quoted string is treated like a double -- some
  1579.      programs (e.g., troff) are perverse this way */
  1580.  
  1581.       if (ident_length)
  1582.     goto specialchar;
  1583.  
  1584.       start_line = ip->lineno;
  1585.  
  1586.       /* Skip ahead to a matching quote.  */
  1587.  
  1588.       while (1) {
  1589.     if (ibp >= limit) {
  1590.       if (traditional) {
  1591.         if (ip->macro != 0) {
  1592.           /* try harder: this string crosses a macro expansion boundary */
  1593.           POPMACRO;
  1594.           RECACHE;
  1595.           continue;
  1596.         }
  1597.       } else
  1598.         error_with_line (line_for_error (start_line),
  1599.                  "unterminated string or character constant");
  1600.       break;
  1601.     }
  1602.     *obp++ = *ibp;
  1603.     switch (*ibp++) {
  1604.     case '\n':
  1605.       ++ip->lineno;
  1606.       ++op->lineno;
  1607.       if (traditional)
  1608.         goto while2end;
  1609.       if (pedantic || c == '\'') {
  1610.         error_with_line (line_for_error (start_line),
  1611.                  "unterminated string or character constant");
  1612.         goto while2end;
  1613.       }
  1614.       break;
  1615.  
  1616.     case '\\':
  1617.       if (ibp >= limit)
  1618.         break;
  1619.       if (*ibp == '\n') {
  1620.         /* Backslash newline is replaced by nothing at all,
  1621.            but keep the line counts correct.  */
  1622.         --obp;
  1623.         ++ibp;
  1624.         ++ip->lineno;
  1625.       } else {
  1626.         /* ANSI stupidly requires that in \\ the second \
  1627.            is *not* prevented from combining with a newline.  */
  1628.         while (*ibp == '\\' && ibp[1] == '\n') {
  1629.           ibp += 2;
  1630.           ++ip->lineno;
  1631.         }
  1632.         *obp++ = *ibp++;
  1633.       }
  1634.       break;
  1635.  
  1636.     case '\"':
  1637.     case '\'':
  1638.       if (ibp[-1] == c)
  1639.         goto while2end;
  1640.       break;
  1641.     }
  1642.       }
  1643.     while2end:
  1644.       break;
  1645.  
  1646.     case '/':
  1647.       if (*ibp == '\\' && ibp[1] == '\n')
  1648.     newline_fix (ibp);
  1649.       if (cplusplus && *ibp == '/') {
  1650.     /* C++ style comment... */
  1651.     start_line = ip->lineno;
  1652.  
  1653.     --ibp;            /* Back over the slash */
  1654.     --obp;
  1655.  
  1656.     /* Comments are equivalent to spaces. */
  1657.     if (! put_out_comments)
  1658.       *obp++ = ' ';
  1659.     else {
  1660.       /* must fake up a comment here */
  1661.       *obp++ = '/';
  1662.       *obp++ = '/';
  1663.     }
  1664.     {
  1665.       U_CHAR *before_bp = ibp+2;
  1666.  
  1667.       while (ibp < limit) {
  1668.         if (*ibp++ == '\n') {
  1669.           ibp--;
  1670.           if (put_out_comments) {
  1671.         bcopy (before_bp, obp, ibp - before_bp);
  1672.         obp += ibp - before_bp;
  1673.           }
  1674.           break;
  1675.         }
  1676.       }
  1677.       break;
  1678.     }
  1679.       }
  1680.       if (*ibp != '*')
  1681.     goto randomchar;
  1682.       if (ip->macro != 0)
  1683.     goto randomchar;
  1684.       if (ident_length)
  1685.     goto specialchar;
  1686.  
  1687.       /* We have a comment.  Skip it, optionally copying it to output.  */
  1688.  
  1689.       start_line = ip->lineno;
  1690.  
  1691.       ++ibp;            /* Skip the star. */
  1692.  
  1693.       /* Comments are equivalent to spaces.
  1694.      Note that we already output the slash; we might not want it.
  1695.      For -traditional, a comment is equivalent to nothing.  */
  1696.       if (! put_out_comments) {
  1697.     if (traditional)
  1698.       obp--;
  1699.     else
  1700.       obp[-1] = ' ';
  1701.       }
  1702.       else
  1703.     *obp++ = '*';
  1704.  
  1705.       {
  1706.     U_CHAR *before_bp = ibp;
  1707.  
  1708.     while (ibp < limit) {
  1709.       switch (*ibp++) {
  1710.       case '/':
  1711.         if (warn_comments && ibp < limit && *ibp == '*')
  1712.           warning("`/*' within comment");
  1713.         break;
  1714.       case '*':
  1715.         if (*ibp == '\\' && ibp[1] == '\n')
  1716.           newline_fix (ibp);
  1717.         if (ibp >= limit || *ibp == '/')
  1718.           goto comment_end;
  1719.         break;
  1720.       case '\n':
  1721.         ++ip->lineno;
  1722.         /* Copy the newline into the output buffer, in order to
  1723.            avoid the pain of a #line every time a multiline comment
  1724.            is seen.  */
  1725.         if (!put_out_comments)
  1726.           *obp++ = '\n';
  1727.         ++op->lineno;
  1728.       }
  1729.     }
  1730.       comment_end:
  1731.  
  1732.     if (ibp >= limit)
  1733.       error_with_line (line_for_error (start_line),
  1734.                "unterminated comment");
  1735.     else {
  1736.       ibp++;
  1737.       if (put_out_comments) {
  1738.         bcopy (before_bp, obp, ibp - before_bp);
  1739.         obp += ibp - before_bp;
  1740.       }
  1741.     }
  1742.       }
  1743.       break;
  1744.  
  1745.     case '$':
  1746.       if (!dollars_in_ident)
  1747.     goto randomchar;
  1748.       goto letter;
  1749.  
  1750.     case '0': case '1': case '2': case '3': case '4':
  1751.     case '5': case '6': case '7': case '8': case '9':
  1752.       /* If digit is not part of identifier, it starts a number,
  1753.      which means that following letters are not an identifier.
  1754.      "0x5" does not refer to an identifier "x5".
  1755.      So copy all alphanumerics that follow without accumulating
  1756.      as an identifier.  Periods also, for sake of "3.e7".  */
  1757.  
  1758.       if (ident_length == 0) {
  1759.     while (ibp < limit) {
  1760.       c = *ibp++;
  1761.       if (!isalnum (c) && c != '.' && c != '_') {
  1762.         --ibp;
  1763.         break;
  1764.       }
  1765.       *obp++ = c;
  1766.       /* A sign can be part of a preprocessing number
  1767.          if it follows an e.  */
  1768.       if (c == 'e' || c == 'E') {
  1769.         if (ibp < limit && (*ibp == '+' || *ibp == '-'))
  1770.           *obp++ = *ibp++;
  1771.       }
  1772.     }
  1773.     break;
  1774.       }
  1775.       /* fall through */
  1776.  
  1777.     case '_':
  1778.     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  1779.     case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  1780.     case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  1781.     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  1782.     case 'y': case 'z':
  1783.     case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  1784.     case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  1785.     case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  1786.     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  1787.     case 'Y': case 'Z':
  1788.     letter:
  1789.       ident_length++;
  1790.       /* Compute step of hash function, to avoid a proc call on every token */
  1791.       hash = HASHSTEP (hash, c);
  1792.       break;
  1793.  
  1794.     case '\n':
  1795.       /* If reprocessing a macro expansion, newline is a special marker.  */
  1796.       if (ip->macro != 0) {
  1797.     /* Newline White is a "funny space" to separate tokens that are
  1798.        supposed to be separate but without space between.
  1799.        Here White means any horizontal whitespace character.
  1800.        Newline - marks a recursive macro use that is not
  1801.        supposed to be expandable.  */
  1802.  
  1803.     if (*ibp == '-') {
  1804.       /* Newline - inhibits expansion of preceding token.
  1805.          If expanding a macro arg, we keep the newline -.
  1806.          In final output, it is deleted.  */
  1807.       if (! concatenated) {
  1808.         ident_length = 0;
  1809.         hash = 0;
  1810.       }
  1811.       ibp++;
  1812.       if (!output_marks) {
  1813.         obp--;
  1814.       } else {
  1815.         /* If expanding a macro arg, keep the newline -.  */
  1816.         *obp++ = '-';
  1817.       }
  1818.     } else if (is_space[*ibp]) {
  1819.       /* Newline Space does not prevent expansion of preceding token
  1820.          so expand the preceding token and then come back.  */
  1821.       if (ident_length > 0)
  1822.         goto specialchar;
  1823.  
  1824.       /* If generating final output, newline space makes a space.  */
  1825.       if (!output_marks) {
  1826.         obp[-1] = *ibp++;
  1827.         /* And Newline Newline makes a newline, so count it.  */
  1828.         if (obp[-1] == '\n')
  1829.           op->lineno++;
  1830.       } else {
  1831.         /* If expanding a macro arg, keep the newline space.
  1832.            If the arg gets stringified, newline space makes nothing.  */
  1833.         *obp++ = *ibp++;
  1834.       }
  1835.     } else abort ();    /* Newline followed by something random?  */
  1836.     break;
  1837.       }
  1838.  
  1839.       /* If there is a pending identifier, handle it and come back here.  */
  1840.       if (ident_length > 0)
  1841.     goto specialchar;
  1842.  
  1843.       beg_of_line = ibp;
  1844.  
  1845.       /* Update the line counts and output a #line if necessary.  */
  1846.       ++ip->lineno;
  1847.       ++op->lineno;
  1848.       if (ip->lineno != op->lineno) {
  1849.     op->bufp = obp;
  1850.     output_line_command (ip, op, 1, same_file);
  1851.     check_expand (op, ip->length - (ip->bufp - ip->buf));
  1852.     obp = op->bufp;
  1853.       }
  1854.       break;
  1855.  
  1856.       /* Come here either after (1) a null character that is part of the input
  1857.      or (2) at the end of the input, because there is a null there.  */
  1858.     case 0:
  1859.       if (ibp <= limit)
  1860.     /* Our input really contains a null character.  */
  1861.     goto randomchar;
  1862.  
  1863.       /* At end of a macro-expansion level, pop it and read next level.  */
  1864.       if (ip->macro != 0) {
  1865.     obp--;
  1866.     ibp--;
  1867.     /* If traditional, and we have an identifier that ends here,
  1868.        process it now, so we get the right error for recursion.  */
  1869.     if (traditional && ident_length
  1870.         && ! is_idchar[*instack[indepth - 1].bufp]) {
  1871.       redo_char = 1;
  1872.       goto randomchar;
  1873.     }
  1874.     POPMACRO;
  1875.     RECACHE;
  1876.     break;
  1877.       }
  1878.  
  1879.       /* If we don't have a pending identifier,
  1880.      return at end of input.  */
  1881.       if (ident_length == 0) {
  1882.     obp--;
  1883.     ibp--;
  1884.     op->bufp = obp;
  1885.     ip->bufp = ibp;
  1886.     goto ending;
  1887.       }
  1888.  
  1889.       /* If we do have a pending identifier, just consider this null
  1890.      a special character and arrange to dispatch on it again.
  1891.      The second time, IDENT_LENGTH will be zero so we will return.  */
  1892.  
  1893.       /* Fall through */
  1894.  
  1895. specialchar:
  1896.  
  1897.       /* Handle the case of a character such as /, ', " or null
  1898.      seen following an identifier.  Back over it so that
  1899.      after the identifier is processed the special char
  1900.      will be dispatched on again.  */
  1901.  
  1902.       ibp--;
  1903.       obp--;
  1904.       redo_char = 1;
  1905.  
  1906.     default:
  1907.  
  1908. randomchar:
  1909.  
  1910.       if (ident_length > 0) {
  1911.     register HASHNODE *hp;
  1912.  
  1913.     /* We have just seen an identifier end.  If it's a macro, expand it.
  1914.  
  1915.        IDENT_LENGTH is the length of the identifier
  1916.        and HASH is its hash code.
  1917.  
  1918.        The identifier has already been copied to the output,
  1919.        so if it is a macro we must remove it.
  1920.  
  1921.        If REDO_CHAR is 0, the char that terminated the identifier
  1922.        has been skipped in the output and the input.
  1923.        OBP-IDENT_LENGTH-1 points to the identifier.
  1924.        If the identifier is a macro, we must back over the terminator.
  1925.  
  1926.        If REDO_CHAR is 1, the terminating char has already been
  1927.        backed over.  OBP-IDENT_LENGTH points to the identifier.  */
  1928.  
  1929.     for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
  1930.          hp = hp->next) {
  1931.  
  1932.       if (hp->length == ident_length) {
  1933.         U_CHAR *obufp_before_macroname;
  1934.         int op_lineno_before_macroname;
  1935.         register int i = ident_length;
  1936.         register U_CHAR *p = hp->name;
  1937.         register U_CHAR *q = obp - i;
  1938.         int disabled;
  1939.  
  1940.         if (! redo_char)
  1941.           q--;
  1942.  
  1943.         do {        /* All this to avoid a strncmp () */
  1944.           if (*p++ != *q++)
  1945.         goto hashcollision;
  1946.         } while (--i);
  1947.  
  1948.         /* We found a use of a macro name.
  1949.            see if the context shows it is a macro call.  */
  1950.  
  1951.         /* Back up over terminating character if not already done.  */
  1952.         if (! redo_char) {
  1953.           ibp--;
  1954.           obp--;
  1955.         }
  1956.  
  1957.         obufp_before_macroname = obp - ident_length;
  1958.         op_lineno_before_macroname = op->lineno;
  1959.  
  1960.         /* Record whether the macro is disabled.  */
  1961.         disabled = hp->type == T_DISABLED;
  1962.  
  1963.         /* This looks like a macro ref, but if the macro was disabled,
  1964.            just copy its name and put in a marker if requested.  */
  1965.  
  1966.         if (disabled) {
  1967. #if 0
  1968.           /* This error check caught useful cases such as
  1969.          #define foo(x,y) bar(x(y,0), y)
  1970.          foo(foo, baz)  */
  1971.           if (traditional)
  1972.         error ("recursive use of macro `%s'", hp->name);
  1973. #endif
  1974.  
  1975.           if (output_marks) {
  1976.         check_expand (op, limit - ibp + 2);
  1977.         *obp++ = '\n';
  1978.         *obp++ = '-';
  1979.           }
  1980.           break;
  1981.         }
  1982.  
  1983.         /* If macro wants an arglist, verify that a '(' follows.
  1984.            first skip all whitespace, copying it to the output
  1985.            after the macro name.  Then, if there is no '(',
  1986.            decide this is not a macro call and leave things that way.  */
  1987.         if ((hp->type == T_MACRO || hp->type == T_DISABLED)
  1988.         && hp->value.defn->nargs >= 0)
  1989.           {
  1990.         while (1) {
  1991.           /* Scan forward over whitespace, copying it to the output.  */
  1992.           if (ibp == limit && ip->macro != 0) {
  1993.             POPMACRO;
  1994.             RECACHE;
  1995.           }
  1996.           /* A comment: copy it to the output unchanged.  */
  1997.           else if (*ibp == '/' && ibp+1 != limit && ibp[1] == '*') {
  1998.             *obp++ = '/';
  1999.             *obp++ = '*';
  2000.             ibp += 2;
  2001.             while (ibp + 1 != limit
  2002.                && !(ibp[0] == '*' && ibp[1] == '/')) {
  2003.               /* We need not worry about newline-marks,
  2004.              since they are never found in comments.  */
  2005.               if (*ibp == '\n') {
  2006.             /* Newline in a file.  Count it.  */
  2007.             ++ip->lineno;
  2008.             ++op->lineno;
  2009.               }
  2010.               *obp++ = *ibp++;
  2011.             }
  2012.             ibp += 2;
  2013.             *obp++ = '*';
  2014.             *obp++ = '/';
  2015.           }
  2016.           else if (is_space[*ibp]) {
  2017.             *obp++ = *ibp++;
  2018.             if (ibp[-1] == '\n') {
  2019.               if (ip->macro == 0) {
  2020.             /* Newline in a file.  Count it.  */
  2021.             ++ip->lineno;
  2022.             ++op->lineno;
  2023.               } else if (!output_marks) {
  2024.             /* A newline mark, and we don't want marks
  2025.                in the output.  If it is newline-hyphen,
  2026.                discard it entirely.  Otherwise, it is
  2027.                newline-whitechar, so keep the whitechar.  */
  2028.             obp--;
  2029.             if (*ibp == '-')
  2030.               ibp++;
  2031.             else {
  2032.               if (*ibp == '\n')
  2033.                 ++op->lineno;
  2034.               *obp++ = *ibp++;
  2035.             }
  2036.               } else {
  2037.             /* A newline mark; copy both chars to the output.  */
  2038.             *obp++ = *ibp++;
  2039.               }
  2040.             }
  2041.           }
  2042.           else break;
  2043.         }
  2044.         if (*ibp != '(')
  2045.           break;
  2046.           }
  2047.  
  2048.         /* This is now known to be a macro call.
  2049.            Discard the macro name from the output,
  2050.            along with any following whitespace just copied.  */
  2051.         obp = obufp_before_macroname;
  2052.         op->lineno = op_lineno_before_macroname;
  2053.  
  2054.         /* Expand the macro, reading arguments as needed,
  2055.            and push the expansion on the input stack.  */
  2056.         ip->bufp = ibp;
  2057.         op->bufp = obp;
  2058.         macroexpand (hp, op);
  2059.  
  2060.         /* Reexamine input stack, since macroexpand has pushed
  2061.            a new level on it.  */
  2062.         obp = op->bufp;
  2063.         RECACHE;
  2064.         break;
  2065.       }
  2066. hashcollision:
  2067.            ;
  2068.     }            /* End hash-table-search loop */
  2069.     ident_length = hash = 0; /* Stop collecting identifier */
  2070.     redo_char = 0;
  2071.     concatenated = 0;
  2072.       }                /* End if (ident_length > 0) */
  2073.     }                /* End switch */
  2074.   }                /* End per-char loop */
  2075.  
  2076.   /* Come here to return -- but first give an error message
  2077.      if there was an unterminated successful conditional.  */
  2078.  ending:
  2079.   if (if_stack != ip->if_stack) {
  2080.     char *str;
  2081.     switch (if_stack->type) {
  2082.     case T_IF:
  2083.       str = "if";
  2084.       break;
  2085.     case T_IFDEF:
  2086.       str = "ifdef";
  2087.       break;
  2088.     case T_IFNDEF:
  2089.       str = "ifndef";
  2090.       break;
  2091.     case T_ELSE:
  2092.       str = "else";
  2093.       break;
  2094.     case T_ELIF:
  2095.       str = "elif";
  2096.       break;
  2097.     }
  2098.     error_with_line (line_for_error (if_stack->lineno),
  2099.              "unterminated #%s conditional", str);
  2100.   }
  2101.   if_stack = ip->if_stack;
  2102. }
  2103.  
  2104. /*
  2105.  * Rescan a string into a temporary buffer and return the result
  2106.  * as a FILE_BUF.  Note this function returns a struct, not a pointer.
  2107.  *
  2108.  * OUTPUT_MARKS nonzero means keep Newline markers found in the input
  2109.  * and insert such markers when appropriate.  See `rescan' for details.
  2110.  * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
  2111.  * before substitution; it is 0 for other uses.
  2112.  */
  2113. FILE_BUF
  2114. expand_to_temp_buffer (buf, limit, output_marks)
  2115.      U_CHAR *buf, *limit;
  2116.      int output_marks;
  2117. {
  2118.   register FILE_BUF *ip;
  2119.   FILE_BUF obuf;
  2120.   int length = limit - buf;
  2121.   U_CHAR *buf1;
  2122.   int odepth = indepth;
  2123.  
  2124.   if (length < 0)
  2125.     abort ();
  2126.  
  2127.   /* Set up the input on the input stack.  */
  2128.  
  2129.   buf1 = (U_CHAR *) alloca (length + 1);
  2130.   {
  2131.     register U_CHAR *p1 = buf;
  2132.     register U_CHAR *p2 = buf1;
  2133.  
  2134.     while (p1 != limit)
  2135.       *p2++ = *p1++;
  2136.   }
  2137.   buf1[length] = 0;
  2138.  
  2139.   /* Set up to receive the output.  */
  2140.  
  2141.   obuf.length = length * 2 + 100; /* Usually enough.  Why be stingy?  */
  2142.   obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
  2143.   obuf.fname = 0;
  2144.   obuf.macro = 0;
  2145.   obuf.free_ptr = 0;
  2146.  
  2147.   CHECK_DEPTH ({return obuf;});
  2148.  
  2149.   ++indepth;
  2150.  
  2151.   ip = &instack[indepth];
  2152.   ip->fname = 0;
  2153.   ip->macro = 0;
  2154.   ip->free_ptr = 0;
  2155.   ip->length = length;
  2156.   ip->buf = ip->bufp = buf1;
  2157.   ip->if_stack = if_stack;
  2158.  
  2159.   ip->lineno = obuf.lineno = 1;
  2160.  
  2161.   /* Scan the input, create the output.  */
  2162.  
  2163.   rescan (&obuf, output_marks);
  2164.  
  2165.   /* Pop input stack to original state.  */
  2166.   --indepth;
  2167.  
  2168.   if (indepth != odepth)
  2169.     abort ();
  2170.  
  2171.   /* Record the output.  */
  2172.   obuf.length = obuf.bufp - obuf.buf;
  2173.  
  2174.   return obuf;
  2175. }
  2176.  
  2177. /*
  2178.  * Process a # directive.  Expects IP->bufp to point to the '#', as in
  2179.  * `#define foo bar'.  Passes to the command handler
  2180.  * (do_define, do_include, etc.): the addresses of the 1st and
  2181.  * last chars of the command (starting immediately after the #
  2182.  * keyword), plus op and the keyword table pointer.  If the command
  2183.  * contains comments it is copied into a temporary buffer sans comments
  2184.  * and the temporary buffer is passed to the command handler instead.
  2185.  * Likewise for backslash-newlines.
  2186.  *
  2187.  * Returns nonzero if this was a known # directive.
  2188.  * Otherwise, returns zero, without advancing the input pointer.
  2189.  */
  2190.  
  2191. int
  2192. handle_directive (ip, op)
  2193.      FILE_BUF *ip, *op;
  2194. {
  2195.   register U_CHAR *bp, *cp;
  2196.   register struct directive *kt;
  2197.   register int ident_length;
  2198.   U_CHAR *resume_p;
  2199.  
  2200.   /* Nonzero means we must copy the entire command
  2201.      to get rid of comments or backslash-newlines.  */
  2202.   int copy_command = 0;
  2203.  
  2204.   U_CHAR *ident, *after_ident;
  2205.  
  2206.   bp = ip->bufp;
  2207.   /* Skip whitespace and \-newline.  */
  2208.   while (1) {
  2209.     if (is_hor_space[*bp])
  2210.       bp++;
  2211.     else if (*bp == '/' && bp[1] == '*') {
  2212.       ip->bufp = bp;
  2213.       skip_to_end_of_comment (ip, &ip->lineno);
  2214.       bp = ip->bufp;
  2215.     } else if (*bp == '\\' && bp[1] == '\n') {
  2216.       bp += 2; ip->lineno++;
  2217.     } else break;
  2218.   }
  2219.  
  2220.   /* Now find end of directive name.
  2221.      If we encounter a backslash-newline, exchange it with any following
  2222.      symbol-constituents so that we end up with a contiguous name.  */
  2223.  
  2224.   cp = bp;
  2225.   while (1) {
  2226.     if (is_idchar[*cp])
  2227.       cp++;
  2228.     else {
  2229.       if (*cp == '\\' && cp[1] == '\n')
  2230.     name_newline_fix (cp);
  2231.       if (is_idchar[*cp])
  2232.     cp++;
  2233.       else break;
  2234.     }
  2235.   }
  2236.   ident_length = cp - bp;
  2237.   ident = bp;
  2238.   after_ident = cp;
  2239.  
  2240.   /* A line of just `#' becomes blank.  */
  2241.  
  2242.   if (traditional && ident_length == 0 && *after_ident == '\n') {
  2243.     ip->bufp = after_ident;
  2244.     return 1;
  2245.   }
  2246.  
  2247.   /*
  2248.    * Decode the keyword and call the appropriate expansion
  2249.    * routine, after moving the input pointer up to the next line.
  2250.    */
  2251.   for (kt = directive_table; kt->length > 0; kt++) {
  2252.     if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length)) {
  2253.       register U_CHAR *buf;
  2254.       register U_CHAR *limit = ip->buf + ip->length;
  2255.       int unterminated = 0;
  2256.  
  2257.       /* Nonzero means do not delete comments within the directive.
  2258.      #define needs this when -traditional.  */
  2259.       int keep_comments = traditional && kt->traditional_comments;
  2260.  
  2261.       /* Find the end of this command (first newline not backslashed
  2262.      and not in a string or comment).
  2263.      Set COPY_COMMAND if the command must be copied
  2264.      (it contains a backslash-newline or a comment).  */
  2265.  
  2266.       buf = bp = after_ident;
  2267.       while (bp < limit) {
  2268.     register U_CHAR c = *bp++;
  2269.     switch (c) {
  2270.     case '\\':
  2271.       if (bp < limit) {
  2272.         if (*bp == '\n') {
  2273.           ip->lineno++;
  2274.           copy_command = 1;
  2275.         }
  2276.         bp++;
  2277.       }
  2278.       break;
  2279.  
  2280.     case '\'':
  2281.     case '\"':
  2282.       bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, ©_command, &unterminated);
  2283.       /* Don't bother calling the directive if we already got an error
  2284.          message due to unterminated string.  Skip everything and pretend
  2285.          we called the directive.  */
  2286.       if (unterminated) {
  2287.         if (traditional) {
  2288.           /* Traditional preprocessing permits unterminated strings.  */
  2289.           --bp;
  2290.           ip->bufp = bp;
  2291.           goto endloop1;
  2292.         }
  2293.         ip->bufp = bp;
  2294.         return 1;
  2295.       }
  2296.       break;
  2297.  
  2298.       /* <...> is special for #include.  */
  2299.     case '<':
  2300.       if (!kt->angle_brackets)
  2301.         break;
  2302.       while (*bp && *bp != '>') bp++;
  2303.       break;
  2304.  
  2305.     case '/':
  2306.       if (*bp == '\\' && bp[1] == '\n')
  2307.         newline_fix (bp);
  2308.       if (*bp == '*'
  2309.           || (cplusplus && *bp == '/')) {
  2310.         U_CHAR *obp = bp - 1;
  2311.         ip->bufp = bp + 1;
  2312.         skip_to_end_of_comment (ip, &ip->lineno);
  2313.         bp = ip->bufp;
  2314.         /* No need to copy the command because of a comment at the end;
  2315.            just don't include the comment in the directive.  */
  2316.         if (bp == limit || *bp == '\n') {
  2317.           bp = obp;
  2318.           goto endloop1;
  2319.         }
  2320.         /* Don't remove the comments if -traditional.  */
  2321.         if (! keep_comments)
  2322.           copy_command++;
  2323.       }
  2324.       break;
  2325.  
  2326.     case '\n':
  2327.       --bp;        /* Point to the newline */
  2328.       ip->bufp = bp;
  2329.       goto endloop1;
  2330.     }
  2331.       }
  2332.       ip->bufp = bp;
  2333.  
  2334.     endloop1:
  2335.       resume_p = ip->bufp;
  2336.       /* BP is the end of the directive.
  2337.      RESUME_P is the next interesting data after the directive.
  2338.      A comment may come between.  */
  2339.  
  2340.       if (copy_command) {
  2341.     register U_CHAR *xp = buf;
  2342.     /* Need to copy entire command into temp buffer before dispatching */
  2343.  
  2344.     cp = (U_CHAR *) alloca (bp - buf + 5); /* room for cmd plus
  2345.                           some slop */
  2346.     buf = cp;
  2347.  
  2348.     /* Copy to the new buffer, deleting comments
  2349.        and backslash-newlines (and whitespace surrounding the latter).  */
  2350.  
  2351.     while (xp < bp) {
  2352.       register U_CHAR c = *xp++;
  2353.       *cp++ = c;
  2354.  
  2355.       switch (c) {
  2356.       case '\n':
  2357.         break;
  2358.  
  2359.         /* <...> is special for #include.  */
  2360.       case '<':
  2361.         if (!kt->angle_brackets)
  2362.           break;
  2363.         while (xp < bp && c != '>') {
  2364.           c = *xp++;
  2365.           *cp++ = c;
  2366.         }
  2367.         break;
  2368.  
  2369.       case '\\':
  2370.         if (*xp == '\n') {
  2371.           xp++;
  2372.           cp--;
  2373.           if (cp != buf && is_space[cp[-1]]) {
  2374.         while (cp != buf && is_space[cp[-1]]) cp--;
  2375.         cp++;
  2376.         SKIP_WHITE_SPACE (xp);
  2377.           } else if (is_space[*xp]) {
  2378.         *cp++ = *xp++;
  2379.         SKIP_WHITE_SPACE (xp);
  2380.           }
  2381.         }
  2382.         break;
  2383.  
  2384.       case '\'':
  2385.       case '\"':
  2386.         if (!traditional) {
  2387.           register U_CHAR *bp1
  2388.         = skip_quoted_string (xp - 1, limit, ip->lineno, 0, 0, 0);
  2389.           while (xp != bp1)
  2390.         *cp++ = *xp++;
  2391.         }
  2392.         break;
  2393.  
  2394.       case '/':
  2395.         if (keep_comments)
  2396.           break;
  2397.         if (*xp == '*'
  2398.         || (cplusplus && *xp == '/')) {
  2399.           if (traditional)
  2400.         cp--;
  2401.           else
  2402.         cp[-1] = ' ';
  2403.           ip->bufp = xp + 1;
  2404.           skip_to_end_of_comment (ip, 0);
  2405.           xp = ip->bufp;
  2406.         }
  2407.       }
  2408.     }
  2409.  
  2410.     /* Null-terminate the copy.  */
  2411.  
  2412.     *cp = 0;
  2413.       }
  2414.       else
  2415.     cp = bp;
  2416.  
  2417.       ip->bufp = resume_p;
  2418.  
  2419.       /* Some directives should be written out for cc1 to process,
  2420.      just as if they were not defined.  */
  2421.  
  2422.       if (kt->pass_thru) {
  2423.         int len;
  2424.  
  2425.     /* Output directive name.  */
  2426.         check_expand (op, kt->length+1);
  2427.         *op->bufp++ = '#';
  2428.         bcopy (kt->name, op->bufp, kt->length);
  2429.         op->bufp += kt->length;
  2430.  
  2431.     /* Output arguments.  */
  2432.         len = (cp - buf);
  2433.         check_expand (op, len);
  2434.         bcopy (buf, op->bufp, len);
  2435.         op->bufp += len;
  2436.       }
  2437.  
  2438.       /* Call the appropriate command handler.  buf now points to
  2439.      either the appropriate place in the input buffer, or to
  2440.      the temp buffer if it was necessary to make one.  cp
  2441.      points to the first char after the contents of the (possibly
  2442.      copied) command, in either case. */
  2443.       (*kt->func) (buf, cp, op, kt);
  2444.       check_expand (op, ip->length - (ip->bufp - ip->buf));
  2445.  
  2446.       return 1;
  2447.     }
  2448.   }
  2449.  
  2450.   return 0;
  2451. }
  2452.  
  2453. static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  2454.                  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  2455.                 };
  2456.  
  2457. /*
  2458.  * expand things like __FILE__.  Place the expansion into the output
  2459.  * buffer *without* rescanning.
  2460.  */
  2461. special_symbol (hp, op)
  2462.      HASHNODE *hp;
  2463.      FILE_BUF *op;
  2464. {
  2465.   char *buf;
  2466.   long t;
  2467.   int i, len;
  2468.   int true_indepth;
  2469.   FILE_BUF *ip = NULL;
  2470.   static struct tm *timebuf = NULL;
  2471.   struct tm *localtime ();
  2472.  
  2473.   int paren = 0;        /* For special `defined' keyword */
  2474.  
  2475.   for (i = indepth; i >= 0; i--)
  2476.     if (instack[i].fname != NULL) {
  2477.       ip = &instack[i];
  2478.       break;
  2479.     }
  2480.   if (ip == NULL) {
  2481.     error ("cccp error: not in any file?!");
  2482.     return;            /* the show must go on */
  2483.   }
  2484.  
  2485.   switch (hp->type) {
  2486.   case T_FILE:
  2487.   case T_BASE_FILE:
  2488.     {
  2489.       char *string;
  2490.       if (hp->type == T_FILE)
  2491.     string = ip->fname;
  2492.       else
  2493.     string = instack[0].fname;
  2494.  
  2495.       if (string)
  2496.     {
  2497. #ifndef atarist
  2498.       buf = (char *) alloca (3 + strlen (string));
  2499.       sprintf (buf, "\"%s\"", string);
  2500. #else
  2501.           buf = (char *) alloca (16 + strlen (string));
  2502.           {
  2503.           char *tbuf = (char *) alloca (16 + strlen (string));
  2504.           (void) slashifying_bcopy(string, tbuf, strlen(string)+1);
  2505.           sprintf (buf, "\"%s\"", tbuf);
  2506.           }
  2507. #endif /* atarist */
  2508.     }
  2509.       else
  2510.     buf = "\"\"";
  2511.  
  2512.       break;
  2513.     }
  2514.  
  2515.   case T_INCLUDE_LEVEL:
  2516.     true_indepth = 0;
  2517.     for (i = indepth; i >= 0; i--)
  2518.       if (instack[i].fname != NULL)
  2519.         true_indepth++;
  2520.  
  2521.     buf = (char *) alloca (8);    /* Eigth bytes ought to be more than enough */
  2522.     sprintf (buf, "%d", true_indepth - 1);
  2523.     break;
  2524.  
  2525.   case T_VERSION:
  2526.     buf = (char *) alloca (3 + strlen (version_string));
  2527.     sprintf (buf, "\"%s\"", version_string);
  2528.     break;
  2529.  
  2530.   case T_CONST:
  2531.     buf = (char *) alloca (4 * sizeof (int));
  2532.     sprintf (buf, "%d", hp->value.ival);
  2533.     break;
  2534.  
  2535.   case T_SPECLINE:
  2536.     buf = (char *) alloca (10);
  2537.     sprintf (buf, "%d", ip->lineno);
  2538.     break;
  2539.  
  2540.   case T_DATE:
  2541.   case T_TIME:
  2542.     if (timebuf == NULL) {
  2543.       t = time (0);
  2544.       timebuf = localtime (&t);
  2545.     }
  2546.     buf = (char *) alloca (20);
  2547.     if (hp->type == T_DATE)
  2548.       sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
  2549.           timebuf->tm_mday, timebuf->tm_year + 1900);
  2550.     else
  2551.       sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
  2552.           timebuf->tm_sec);
  2553.     break;
  2554.  
  2555.   case T_SPEC_DEFINED:
  2556.     buf = " 0 ";        /* Assume symbol is not defined */
  2557.     ip = &instack[indepth];
  2558.     SKIP_WHITE_SPACE (ip->bufp);
  2559.     if (*ip->bufp == '(') {
  2560.       paren++;
  2561.       ip->bufp++;            /* Skip over the paren */
  2562.       SKIP_WHITE_SPACE (ip->bufp);
  2563.     }
  2564.  
  2565.     if (!is_idstart[*ip->bufp])
  2566.       goto oops;
  2567.     if (lookup (ip->bufp, -1, -1))
  2568.       buf = " 1 ";
  2569.     while (is_idchar[*ip->bufp])
  2570.       ++ip->bufp;
  2571.     SKIP_WHITE_SPACE (ip->bufp);
  2572.     if (paren) {
  2573.       if (*ip->bufp != ')')
  2574.     goto oops;
  2575.       ++ip->bufp;
  2576.     }
  2577.     break;
  2578.  
  2579. oops:
  2580.  
  2581.     error ("`defined' must be followed by ident or (ident)");
  2582.     break;
  2583.  
  2584.   default:
  2585.     error ("cccp error: invalid special hash type"); /* time for gdb */
  2586.     abort ();
  2587.   }
  2588.   len = strlen (buf);
  2589.   check_expand (op, len);
  2590.   bcopy (buf, op->bufp, len);
  2591.   op->bufp += len;
  2592.  
  2593.   return;
  2594. }
  2595.  
  2596.  
  2597. /* Routines to handle #directives */
  2598.  
  2599. /*
  2600.  * Process include file by reading it in and calling rescan.
  2601.  * Expects to see "fname" or <fname> on the input.
  2602.  */
  2603.  
  2604. do_include (buf, limit, op, keyword)
  2605.      U_CHAR *buf, *limit;
  2606.      FILE_BUF *op;
  2607.      struct directive *keyword;
  2608. {
  2609.   char *fname;        /* Dynamically allocated fname buffer */
  2610.   U_CHAR *fbeg, *fend;        /* Beginning and end of fname */
  2611.  
  2612.   struct file_name_list *stackp = include; /* Chain of dirs to search */
  2613.   struct file_name_list dsp[1];    /* First in chain, if #include "..." */
  2614.   int flen;
  2615.  
  2616.   int f;            /* file number */
  2617.  
  2618.   int retried = 0;        /* Have already tried macro
  2619.                    expanding the include line*/
  2620.   FILE_BUF trybuf;        /* It got expanded into here */
  2621.   int system_header_p = 0;    /* 0 for "...", 1 for <...> */
  2622.  
  2623.   f= -1;            /* JF we iz paranoid! */
  2624.  
  2625. get_filename:
  2626.  
  2627.   fbeg = buf;
  2628.   SKIP_WHITE_SPACE (fbeg);
  2629.   /* Discard trailing whitespace so we can easily see
  2630.      if we have parsed all the significant chars we were given.  */
  2631.   while (limit != fbeg && is_hor_space[limit[-1]]) limit--;
  2632.  
  2633.   switch (*fbeg++) {
  2634.   case '\"':
  2635.     fend = fbeg;
  2636.     while (fend != limit && *fend != '\"')
  2637.       fend++;
  2638.     if (*fend == '\"' && fend + 1 == limit) {
  2639.       FILE_BUF *fp;
  2640.  
  2641.       /* We have "filename".  Figure out directory this source
  2642.      file is coming from and put it on the front of the list. */
  2643.  
  2644.       /* If -I- was specified, don't search current dir, only spec'd ones. */
  2645.       if (ignore_srcdir) break;
  2646.  
  2647.       for (fp = &instack[indepth]; fp >= instack; fp--)
  2648.     {
  2649.       int n;
  2650.       char *ep,*nam;
  2651.       extern char *rindex ();
  2652.  
  2653.       if ((nam = fp->fname) != NULL) {
  2654.         /* Found a named file.  Figure out dir of the file,
  2655.            and put it in front of the search list.  */
  2656.         dsp[0].next = stackp;
  2657.         stackp = dsp;
  2658. #ifdef atarist
  2659.           if((ep = rindex (nam, '\\')) == NULL)
  2660.           ep = rindex(nam, '/');    /* allow both */
  2661. #else
  2662. #ifndef VMS
  2663.         ep = rindex (nam, '/');
  2664. #else                /* VMS */
  2665.         ep = rindex (nam, ']');
  2666.         if (ep == NULL) ep = rindex (nam, '>');
  2667.         if (ep == NULL) ep = rindex (nam, ':');
  2668.         if (ep != NULL) ep++;
  2669. #endif                /* VMS */
  2670. #endif /* atarist */
  2671.         if (ep != NULL) {
  2672.           n = ep - nam;
  2673.           dsp[0].fname = (char *) alloca (n + 1);
  2674.           strncpy (dsp[0].fname, nam, n);
  2675.           dsp[0].fname[n] = '\0';
  2676.           if (n > max_include_len) max_include_len = n;
  2677.         } else {
  2678.           dsp[0].fname = 0; /* Current directory */
  2679.         }
  2680.         break;
  2681.       }
  2682.     }
  2683.       break;
  2684.     }
  2685.     goto fail;
  2686.  
  2687.   case '<':
  2688.     fend = fbeg;
  2689.     while (fend != limit && *fend != '>') fend++;
  2690.     if (*fend == '>' && fend + 1 == limit) {
  2691.       system_header_p = 1;
  2692.       /* If -I-, start with the first -I dir after the -I-.  */
  2693.       if (first_bracket_include)
  2694.     stackp = first_bracket_include;
  2695.       break;
  2696.     }
  2697.     goto fail;
  2698.  
  2699.   default:
  2700.   fail:
  2701.     if (retried) {
  2702.       error ("#include expects \"fname\" or <fname>");
  2703.       return;
  2704.     } else {
  2705.       trybuf = expand_to_temp_buffer (buf, limit, 0);
  2706.       buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  2707.       bcopy (trybuf.buf, buf, trybuf.bufp - trybuf.buf);
  2708.       limit = buf + (trybuf.bufp - trybuf.buf);
  2709.       free (trybuf.buf);
  2710.       retried++;
  2711.       goto get_filename;
  2712.     }
  2713.   }
  2714.  
  2715.   flen = fend - fbeg;
  2716.   fname = (char *) alloca (max_include_len + flen + 2);
  2717.   /* + 2 above for slash and terminating null.  */
  2718.  
  2719.   /* If specified file name is absolute, just open it.  */
  2720.  
  2721. #ifdef atarist
  2722.   if ((*fbeg == '\\') || (*fbeg == '/')) {
  2723. #else
  2724.   if (*fbeg == '/') {
  2725. #endif
  2726.     strncpy (fname, fbeg, flen);
  2727.     fname[flen] = 0;
  2728.     f = open (fname, O_RDONLY, 0666);
  2729.   } else {
  2730.     /* Search directory path, trying to open the file.
  2731.        Copy each filename tried into FNAME.  */
  2732.  
  2733.     for (; stackp; stackp = stackp->next) {
  2734.       if (stackp->fname) {
  2735.     strcpy (fname, stackp->fname);
  2736. #ifdef atarist
  2737.     strcat (fname, "\\");
  2738. #else
  2739.     strcat (fname, "/");
  2740. #endif
  2741.     fname[strlen (fname) + flen] = 0;
  2742.       } else {
  2743.     fname[0] = 0;
  2744.       }
  2745.       strncat (fname, fbeg, flen);
  2746. #ifdef VMS
  2747.       /* Change this 1/2 Unix 1/2 VMS file specification into a
  2748.          full VMS file specification */
  2749.       if (stackp->fname && (stackp->fname[0] != 0)) {
  2750.     /* Fix up the filename */
  2751.     hack_vms_include_specification (fname);
  2752.       } else {
  2753.           /* This is a normal VMS filespec, so use it unchanged.  */
  2754.     strncpy (fname, fbeg, flen);
  2755.     fname[flen] = 0;
  2756.       }
  2757. #endif /* VMS */
  2758.       if ((f = open (fname, O_RDONLY, 0666)) >= 0)
  2759.     break;
  2760.     }
  2761.   }
  2762.  
  2763.   if (f < 0) {
  2764.     strncpy (fname, fbeg, flen);
  2765.     fname[flen] = 0;
  2766.     error_from_errno (fname);
  2767.  
  2768.     /* For -M, add this file to the dependencies.  */
  2769.     if (print_deps > (system_header_p || (system_include_depth > 0))) {
  2770.       if (system_header_p)
  2771.     warning ("nonexistent file <%.*s> omitted from dependency output",
  2772.          fend - fbeg, fbeg);
  2773.       else
  2774.     {
  2775.       deps_output (fbeg, fend - fbeg);
  2776.       deps_output (" ", 0);
  2777.     }
  2778.     }
  2779.   } else {
  2780.  
  2781.     /* Check to see if this include file is a once-only include file.
  2782.        If so, give up.  */
  2783.  
  2784.     struct file_name_list* ptr;
  2785.  
  2786.     for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
  2787.       if (!strcmp (ptr->fname, fname)) {
  2788.     close (f);
  2789.         return;                /* This file was once'd. */
  2790.       }
  2791.     }
  2792.  
  2793.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  2794.       if (!strcmp (ptr->fname, fname))
  2795.         break;                /* This file was included before. */
  2796.     }
  2797.  
  2798.     if (ptr == 0) {
  2799.       /* This is the first time for this file.  */
  2800.       /* Add it to list of files included.  */
  2801.  
  2802.       ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  2803.       ptr->next = all_include_files;
  2804.       all_include_files = ptr;
  2805.       ptr->fname = savestring (fname);
  2806.  
  2807.       /* For -M, add this file to the dependencies.  */
  2808.       if (print_deps > (system_header_p || (system_include_depth > 0))) {
  2809.     deps_output (fname, strlen (fname));
  2810.     deps_output (" ", 0);
  2811.       }
  2812.     }   
  2813.  
  2814.     if (system_header_p)
  2815.       system_include_depth++;
  2816.  
  2817.     /* Actually process the file.  */
  2818.     finclude (f, fname, op);
  2819.  
  2820.     if (system_header_p)
  2821.       system_include_depth--;
  2822.  
  2823.     close (f);
  2824.   }
  2825. }
  2826.  
  2827. /* Process the contents of include file FNAME, already open on descriptor F,
  2828.    with output to OP.  */
  2829.  
  2830. finclude (f, fname, op)
  2831.      int f;
  2832.      char *fname;
  2833.      FILE_BUF *op;
  2834. {
  2835.   int st_mode;
  2836.   long st_size;
  2837.   long i;
  2838.   FILE_BUF *fp;            /* For input stack frame */
  2839.   int success = 0;
  2840.  
  2841.   CHECK_DEPTH (return;);
  2842.  
  2843.   if (file_size_and_mode (f, &st_mode, &st_size) < 0)
  2844.     goto nope;        /* Impossible? */
  2845.  
  2846.   fp = &instack[indepth + 1];
  2847.   bzero (fp, sizeof (FILE_BUF));
  2848.   fp->fname = fname;
  2849.   fp->length = 0;
  2850.   fp->lineno = 1;
  2851.   fp->if_stack = if_stack;
  2852.  
  2853.   if (st_mode & S_IFREG) {
  2854.     fp->buf = (U_CHAR *) alloca (st_size + 2);
  2855.     fp->bufp = fp->buf;
  2856.  
  2857.     /* Read the file contents, knowing that st_size is an upper bound
  2858.        on the number of bytes we can read.  */
  2859.     while (st_size > 0) {
  2860. #ifdef atarist
  2861.     i = eunuchs_read (f, fp->buf + fp->length, st_size);
  2862. #else
  2863.     i = read (f, fp->buf + fp->length, st_size);
  2864. #endif
  2865.       if (i <= 0) {
  2866.     if (i == 0) break;
  2867.     goto nope;
  2868.       }
  2869.       fp->length += i;
  2870.       st_size -= i;
  2871.     }
  2872.   }
  2873.   else {
  2874.     /* Cannot count its file size before reading.
  2875.        First read the entire file into heap and
  2876.        copy them into buffer on stack. */
  2877.  
  2878.     U_CHAR *bufp;
  2879.     U_CHAR *basep;
  2880.     int bsize = 2000;
  2881.  
  2882.     st_size = 0;
  2883.     basep = (U_CHAR *) xmalloc (bsize + 2);
  2884.     bufp = basep;
  2885.  
  2886.     for (;;) {
  2887. #ifdef atarist
  2888.     i = eunuchs_read (f, bufp, bsize - st_size);
  2889. #else
  2890.       i = read (f, bufp, bsize - st_size);
  2891. #endif
  2892.       if (i < 0)
  2893.     goto nope;      /* error! */
  2894.       if (i == 0)
  2895.     break;    /* End of file */
  2896.       st_size += i;
  2897.       bufp += i;
  2898.       if (bsize == st_size) {    /* Buffer is full! */
  2899.       bsize *= 2;
  2900.       basep = (U_CHAR *) xrealloc (basep, bsize + 2);
  2901.       bufp = basep + st_size;    /* May have moved */
  2902.     }
  2903.     }
  2904.     fp->buf = (U_CHAR *) alloca (st_size + 2);
  2905.     fp->bufp = fp->buf;
  2906.     bcopy (basep, fp->buf, st_size);
  2907.     fp->length = st_size;
  2908.     free (basep);
  2909.   }
  2910.  
  2911.   if (!no_trigraphs)
  2912.     trigraph_pcp (fp);
  2913.  
  2914.   if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
  2915.     fp->buf[fp->length++] = '\n';
  2916.   fp->buf[fp->length] = '\0';
  2917.  
  2918.   success = 1;
  2919.   indepth++;
  2920.  
  2921.   output_line_command (fp, op, 0, enter_file);
  2922.   rescan (op, 0);
  2923.   indepth--;
  2924.   output_line_command (&instack[indepth], op, 0, leave_file);
  2925.  
  2926. nope:
  2927.  
  2928.   if (!success)
  2929.     perror_with_name (fname);
  2930.  
  2931.   close (f);
  2932. }
  2933.  
  2934. /* The arglist structure is built by do_define to tell
  2935.    collect_definition where the argument names begin.  That
  2936.    is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
  2937.    would contain pointers to the strings x, y, and z.
  2938.    Collect_definition would then build a DEFINITION node,
  2939.    with reflist nodes pointing to the places x, y, and z had
  2940.    appeared.  So the arglist is just convenience data passed
  2941.    between these two routines.  It is not kept around after
  2942.    the current #define has been processed and entered into the
  2943.    hash table. */
  2944.  
  2945. struct arglist {
  2946.   struct arglist *next;
  2947.   U_CHAR *name;
  2948.   int length;
  2949.   int argno;
  2950. };
  2951.  
  2952. /* Process a #define command.
  2953. BUF points to the contents of the #define command, as a continguous string.
  2954. LIMIT points to the first character past the end of the definition.
  2955. KEYWORD is the keyword-table entry for #define.  */
  2956.  
  2957. do_define (buf, limit, op, keyword)
  2958.      U_CHAR *buf, *limit;
  2959.      FILE_BUF *op;
  2960.      struct directive *keyword;
  2961. {
  2962.   U_CHAR *bp;            /* temp ptr into input buffer */
  2963.   U_CHAR *symname;        /* remember where symbol name starts */
  2964.   int sym_length;        /* and how long it is */
  2965.  
  2966.   DEFINITION *defn;
  2967.   int arglengths = 0;        /* Accumulate lengths of arg names
  2968.                    plus number of args.  */
  2969.   int hashcode;
  2970.  
  2971.   bp = buf;
  2972.  
  2973.   while (is_hor_space[*bp])
  2974.     bp++;
  2975.   if (!is_idstart[*bp])
  2976.     warning ("macro name starts with a digit");
  2977.  
  2978.   symname = bp;            /* remember where it starts */
  2979.   while (is_idchar[*bp] && bp < limit) {
  2980.     bp++;
  2981.   }
  2982.   sym_length = bp - symname;
  2983.  
  2984.   /* lossage will occur if identifiers or control keywords are broken
  2985.      across lines using backslash.  This is not the right place to take
  2986.      care of that. */
  2987.  
  2988.   if (*bp == '(') {
  2989.     struct arglist *arg_ptrs = NULL;
  2990.     int argno = 0;
  2991.  
  2992.     bp++;            /* skip '(' */
  2993.     SKIP_WHITE_SPACE (bp);
  2994.  
  2995.     /* Loop over macro argument names.  */
  2996.     while (*bp != ')') {
  2997.       struct arglist *temp;
  2998.  
  2999.       temp = (struct arglist *) alloca (sizeof (struct arglist));
  3000.       temp->name = bp;
  3001.       temp->next = arg_ptrs;
  3002.       temp->argno = argno++;
  3003.       arg_ptrs = temp;
  3004.  
  3005.       if (!is_idstart[*bp])
  3006.     warning ("parameter name starts with a digit in #define");
  3007.  
  3008.       /* Find the end of the arg name.  */
  3009.       while (is_idchar[*bp]) {
  3010.     bp++;
  3011.       }
  3012.       temp->length = bp - temp->name;
  3013.       arglengths += temp->length + 2;
  3014.       SKIP_WHITE_SPACE (bp);
  3015.       if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
  3016.     error ("badly punctuated parameter list in #define");
  3017.     goto nope;
  3018.       }
  3019.       if (*bp == ',') {
  3020.     bp++;
  3021.     SKIP_WHITE_SPACE (bp);
  3022.       }
  3023.       if (bp >= limit) {
  3024.     error ("unterminated parameter list in #define");
  3025.     goto nope;
  3026.       }
  3027.     }
  3028.  
  3029.     ++bp;            /* skip paren */
  3030.     /* Skip exactly one space or tab if any.  */
  3031.     if (bp < limit && (*bp == ' ' || *bp == '\t')) ++bp;
  3032.     /* now everything from bp before limit is the definition. */
  3033.     defn = collect_expansion (bp, limit, argno, arg_ptrs);
  3034.  
  3035.     /* Now set defn->argnames to the result of concatenating
  3036.        the argument names in reverse order
  3037.        with comma-space between them.  */
  3038.     defn->argnames = (U_CHAR *) xmalloc (arglengths + 1);
  3039.     {
  3040.       struct arglist *temp;
  3041.       int i = 0;
  3042.       for (temp = arg_ptrs; temp; temp = temp->next) {
  3043.     bcopy (temp->name, &defn->argnames[i], temp->length);
  3044.     i += temp->length;
  3045.     if (temp->next != 0) {
  3046.       defn->argnames[i++] = ',';
  3047.       defn->argnames[i++] = ' ';
  3048.     }
  3049.       }
  3050.       defn->argnames[i] = 0;
  3051.     }
  3052.   } else {
  3053.     /* simple expansion or empty definition; gobble it */
  3054.     if (is_hor_space[*bp])
  3055.       ++bp;        /* skip exactly one blank/tab char */
  3056.     /* now everything from bp before limit is the definition. */
  3057.     defn = collect_expansion (bp, limit, -1, 0);
  3058.     defn->argnames = (U_CHAR *) "";
  3059.   }
  3060.  
  3061.   hashcode = hashf (symname, sym_length, HASHSIZE);
  3062.  
  3063.   {
  3064.     HASHNODE *hp;
  3065.     if ((hp = lookup (symname, sym_length, hashcode)) != NULL) {
  3066.       if (hp->type != T_MACRO
  3067.       || compare_defs (defn, hp->value.defn)) {
  3068.     U_CHAR *msg;            /* what pain... */
  3069.     msg = (U_CHAR *) alloca (sym_length + 20);
  3070.     bcopy (symname, msg, sym_length);
  3071.     strcpy ((char *) (msg + sym_length), " redefined");
  3072.     warning (msg);
  3073.       }
  3074.       /* Replace the old definition.  */
  3075.       hp->type = T_MACRO;
  3076.       hp->value.defn = defn;
  3077.     } else
  3078.       install (symname, sym_length, T_MACRO, defn, hashcode);
  3079.   }
  3080.  
  3081.   return 0;
  3082.  
  3083. nope:
  3084.  
  3085.   return 1;
  3086. }
  3087.  
  3088. /*
  3089.  * return zero if two DEFINITIONs are isomorphic
  3090.  */
  3091. int
  3092. compare_defs (d1, d2)
  3093.      DEFINITION *d1, *d2;
  3094. {
  3095.   register struct reflist *a1, *a2;
  3096.   register U_CHAR *p1 = d1->expansion;
  3097.   register U_CHAR *p2 = d2->expansion;
  3098.   int first = 1;
  3099.  
  3100.   if (d1->nargs != d2->nargs)
  3101.     return 1;
  3102.   if (strcmp ((char *)d1->argnames, (char *)d2->argnames))
  3103.     return 1;
  3104.   for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
  3105.        a1 = a1->next, a2 = a2->next) {
  3106.     if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
  3107.       || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
  3108.     || a1->argno != a2->argno
  3109.     || a1->stringify != a2->stringify
  3110.     || a1->raw_before != a2->raw_before
  3111.     || a1->raw_after != a2->raw_after)
  3112.       return 1;
  3113.     first = 0;
  3114.     p1 += a1->nchars;
  3115.     p2 += a2->nchars;
  3116.   }
  3117.   if (a1 != a2)
  3118.     return 1;
  3119.   if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
  3120.              p2, d2->length - (p2 - d2->expansion), 1))
  3121.     return 1;
  3122.   return 0;
  3123. }
  3124.  
  3125. /* Return 1 if two parts of two macro definitions are effectively different.
  3126.    One of the parts starts at BEG1 and has LEN1 chars;
  3127.    the other has LEN2 chars at BEG2.
  3128.    Any sequence of whitespace matches any other sequence of whitespace.
  3129.    FIRST means these parts are the first of a macro definition;
  3130.     so ignore leading whitespace entirely.
  3131.    LAST means these parts are the last of a macro definition;
  3132.     so ignore trailing whitespace entirely.  */
  3133.  
  3134. comp_def_part (first, beg1, len1, beg2, len2, last)
  3135.      int first;
  3136.      U_CHAR *beg1, *beg2;
  3137.      int len1, len2;
  3138.      int last;
  3139. {
  3140.   register U_CHAR *end1 = beg1 + len1;
  3141.   register U_CHAR *end2 = beg2 + len2;
  3142.   if (first) {
  3143.     while (beg1 != end1 && is_space[*beg1]) beg1++;
  3144.     while (beg2 != end2 && is_space[*beg2]) beg2++;
  3145.   }
  3146.   if (last) {
  3147.     while (beg1 != end1 && is_space[end1[-1]]) end1--;
  3148.     while (beg2 != end2 && is_space[end2[-1]]) end2--;
  3149.   }
  3150.   while (beg1 != end1 && beg2 != end2) {
  3151.     if (is_space[*beg1] && is_space[*beg2]) {
  3152.       while (beg1 != end1 && is_space[*beg1]) beg1++;
  3153.       while (beg2 != end2 && is_space[*beg2]) beg2++;
  3154.     } else if (*beg1 == *beg2) {
  3155.       beg1++; beg2++;
  3156.     } else break;
  3157.   }
  3158.   return (beg1 != end1) || (beg2 != end2);
  3159. }
  3160.  
  3161. /* Read a replacement list for a macro with parameters.
  3162.    Build the DEFINITION structure.
  3163.    Reads characters of text starting at BUF until LIMIT.
  3164.    ARGLIST specifies the formal parameters to look for
  3165.    in the text of the definition; NARGS is the number of args
  3166.    in that list, or -1 for a macro name that wants no argument list.
  3167.    MACRONAME is the macro name itself (so we can avoid recursive expansion)
  3168.    and NAMELEN is its length in characters.
  3169.    
  3170. Note that comments and backslash-newlines have already been deleted
  3171. from the argument.  */
  3172.  
  3173. /* Leading and trailing Space, Tab, etc. are converted to markers
  3174.    Newline Space, Newline Tab, etc.
  3175.    Newline Space makes a space in the final output
  3176.    but is discarded if stringified.  (Newline Tab is similar but
  3177.    makes a Tab instead.)
  3178.  
  3179.    If there is no trailing whitespace, a Newline Space is added at the end
  3180.    to prevent concatenation that would be contrary to the standard.  */
  3181.  
  3182. DEFINITION *
  3183. collect_expansion (buf, end, nargs, arglist)
  3184.      U_CHAR *buf, *end;
  3185.      int nargs;
  3186.      struct arglist *arglist;
  3187. {
  3188.   DEFINITION *defn;
  3189.   register U_CHAR *p, *limit, *lastp, *exp_p;
  3190.   struct reflist *endpat = NULL;
  3191.   /* Pointer to first nonspace after last ## seen.  */
  3192.   U_CHAR *concat = 0;
  3193.   /* Pointer to first nonspace after last single-# seen.  */
  3194.   U_CHAR *stringify = 0;
  3195.   int maxsize;
  3196.   int expected_delimiter = '\0';
  3197.  
  3198.   /* Scan thru the replacement list, ignoring comments and quoted
  3199.      strings, picking up on the macro calls.  It does a linear search
  3200.      thru the arg list on every potential symbol.  Profiling might say
  3201.      that something smarter should happen. */
  3202.  
  3203.   if (end < buf)
  3204.     abort ();
  3205.  
  3206.   /* Find the beginning of the trailing whitespace.  */
  3207.   /* Find end of leading whitespace.  */
  3208.   limit = end;
  3209.   p = buf;
  3210.   while (p < limit && is_space[limit[-1]]) limit--;
  3211.   while (p < limit && is_space[*p]) p++;
  3212.  
  3213.   /* Allocate space for the text in the macro definition.
  3214.      Leading and trailing whitespace chars need 2 bytes each.
  3215.      Each other input char may or may not need 1 byte,
  3216.      so this is an upper bound.
  3217.      The extra 2 are for invented trailing newline-marker and final null.  */
  3218.   maxsize = (sizeof (DEFINITION)
  3219.          + 2 * (end - limit) + 2 * (p - buf)
  3220.          + (limit - p) + 3);
  3221.   defn = (DEFINITION *) xcalloc (1, maxsize);
  3222.  
  3223.   defn->nargs = nargs;
  3224.   exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
  3225.   lastp = exp_p;
  3226.  
  3227.   p = buf;
  3228.  
  3229.   /* Convert leading whitespace to Newline-markers.  */
  3230.   while (p < limit && is_space[*p]) {
  3231.     *exp_p++ = '\n';
  3232.     *exp_p++ = *p++;
  3233.   }
  3234.  
  3235.   /* Process the main body of the definition.  */
  3236.   while (p < limit) {
  3237.     int skipped_arg = 0;
  3238.     register U_CHAR c = *p++;
  3239.  
  3240.     *exp_p++ = c;
  3241.  
  3242.     if (!traditional) {
  3243.       switch (c) {
  3244.       case '\'':
  3245.       case '\"':
  3246.     for (; p < limit && *p != c; p++) {
  3247.       *exp_p++ = *p;
  3248.       if (*p == '\\') {
  3249.         *exp_p++ = *++p;
  3250.       }
  3251.     }
  3252.     *exp_p++ = *p++;
  3253.     break;
  3254.  
  3255.     /* Special hack: if a \# is written in the #define
  3256.        include a # in the definition.  This is useless for C code
  3257.        but useful for preprocessing other things.  */
  3258.  
  3259.       case '\\':
  3260.     if (p < limit && *p == '#') {
  3261.       /* Pass through this # */
  3262.       exp_p--;
  3263.       *exp_p++ = *p++;
  3264.     } else if (p < limit) {
  3265.       /* Otherwise backslash goes through but makes next char ordinary.  */
  3266.       *exp_p++ = *p++;
  3267.     }
  3268.     break;
  3269.  
  3270.       case '#':
  3271.     if (p < limit && *p == '#') {
  3272.       /* ##: concatenate preceding and following tokens.  */
  3273.       /* Take out the first #, discard preceding whitespace.  */
  3274.       exp_p--;
  3275.       while (exp_p > lastp && is_hor_space[exp_p[-1]])
  3276.         --exp_p;
  3277.       /* Skip the second #.  */
  3278.       p++;
  3279.       /* Discard following whitespace.  */
  3280.       SKIP_WHITE_SPACE (p);
  3281.       concat = p;
  3282.     } else {
  3283.       /* Single #: stringify following argument ref.
  3284.          Don't leave the # in the expansion.  */
  3285.       exp_p--;
  3286.       SKIP_WHITE_SPACE (p);
  3287.       if (p == limit || ! is_idstart[*p] || nargs <= 0)
  3288.         error ("# operator should be followed by a macro argument name\n");
  3289.       else
  3290.         stringify = p;
  3291.     }
  3292.     break;
  3293.       }
  3294.     } else {
  3295.       /* In -traditional mode, recognize arguments inside strings and
  3296.      and character constants, and ignore special properties of #.
  3297.      Arguments inside strings are considered "stringified", but no
  3298.      extra quote marks are supplied.  */
  3299.       switch (c) {
  3300.       case '\'':
  3301.       case '\"':
  3302.     if (expected_delimiter != '\0') {
  3303.       if (c == expected_delimiter)
  3304.         expected_delimiter = '\0';
  3305.     } else
  3306.       expected_delimiter = c;
  3307.     break;
  3308.  
  3309.       case '\\':
  3310.     /* Backslash quotes delimiters and itself, but not macro args.  */
  3311.     if (expected_delimiter != 0 && p < limit
  3312.         && (*p == expected_delimiter || *p == '\\')) {
  3313.       *exp_p++ = *p++;
  3314.       continue;
  3315.     }
  3316.     break;
  3317.  
  3318.       case '/':
  3319.     if (expected_delimiter != '\0') /* No comments inside strings.  */
  3320.       break;
  3321.     if (*p == '*') {
  3322.       /* If we find a comment that wasn't removed by handle_directive,
  3323.          this must be -traditional.  So replace the comment with
  3324.          nothing at all.  */
  3325.       exp_p--;
  3326.       p += 1;
  3327.       while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
  3328.         p++;
  3329.       /* Mark this as a concatenation-point, as if it had been ##.  */
  3330.       concat = p;
  3331.     }
  3332.     break;
  3333.       }
  3334.     }
  3335.  
  3336.     if (is_idchar[c] && nargs > 0) {
  3337.       U_CHAR *id_beg = p - 1;
  3338.       int id_len;
  3339.  
  3340.       --exp_p;
  3341.       while (p != limit && is_idchar[*p]) p++;
  3342.       id_len = p - id_beg;
  3343.  
  3344.       if (is_idstart[c]) {
  3345.     register struct arglist *arg;
  3346.  
  3347.     for (arg = arglist; arg != NULL; arg = arg->next) {
  3348.       struct reflist *tpat;
  3349.  
  3350.       if (arg->name[0] == c
  3351.           && arg->length == id_len
  3352.           && strncmp (arg->name, id_beg, id_len) == 0) {
  3353.         /* make a pat node for this arg and append it to the end of
  3354.            the pat list */
  3355.         tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
  3356.         tpat->next = NULL;
  3357.         tpat->raw_before = concat == id_beg;
  3358.         tpat->raw_after = 0;
  3359.         tpat->stringify = (traditional ? expected_delimiter != '\0'
  3360.                    : stringify == id_beg);
  3361.  
  3362.         if (endpat == NULL)
  3363.           defn->pattern = tpat;
  3364.         else
  3365.           endpat->next = tpat;
  3366.         endpat = tpat;
  3367.  
  3368.         tpat->argno = arg->argno;
  3369.         tpat->nchars = exp_p - lastp;
  3370.         {
  3371.           register U_CHAR *p1 = p;
  3372.           SKIP_WHITE_SPACE (p1);
  3373.           if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
  3374.         tpat->raw_after = 1;
  3375.         }
  3376.         lastp = exp_p;    /* place to start copying from next time */
  3377.         skipped_arg = 1;
  3378.         break;
  3379.       }
  3380.     }
  3381.       }
  3382.  
  3383.       /* If this was not a macro arg, copy it into the expansion.  */
  3384.       if (! skipped_arg) {
  3385.     register U_CHAR *lim1 = p;
  3386.     p = id_beg;
  3387.     while (p != lim1)
  3388.       *exp_p++ = *p++;
  3389.     if (stringify == id_beg)
  3390.       error ("# operator should be followed by a macro argument name\n");
  3391.       }
  3392.     }
  3393.   }
  3394.  
  3395.   if (limit < end) {
  3396.     /* Convert trailing whitespace to Newline-markers.  */
  3397.     while (limit < end && is_space[*limit]) {
  3398.       *exp_p++ = '\n';
  3399.       *exp_p++ = *limit++;
  3400.     }
  3401.   } else if (!traditional) {
  3402.     /* There is no trailing whitespace, so invent some.  */
  3403.     *exp_p++ = '\n';
  3404.     *exp_p++ = ' ';
  3405.   }
  3406.  
  3407.   *exp_p = '\0';
  3408.  
  3409.   defn->length = exp_p - defn->expansion;
  3410.  
  3411.   /* Crash now if we overrun the allocated size.  */
  3412.   if (defn->length + 1 > maxsize)
  3413.     abort ();
  3414.  
  3415. #if 0
  3416. /* This isn't worth the time it takes.  */
  3417.   /* give back excess storage */
  3418.   defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
  3419. #endif
  3420.  
  3421.   return defn;
  3422. }
  3423.  
  3424. /*
  3425.  * interpret #line command.  Remembers previously seen fnames
  3426.  * in its very own hash table.
  3427.  */
  3428. #define FNAME_HASHSIZE 37
  3429.  
  3430. do_line (buf, limit, op, keyword)
  3431.      U_CHAR *buf, *limit;
  3432.      FILE_BUF *op;
  3433.      struct directive *keyword;
  3434. {
  3435.   register U_CHAR *bp;
  3436.   FILE_BUF *ip = &instack[indepth];
  3437.   FILE_BUF tem;
  3438.   int new_lineno;
  3439.   enum file_change_code file_change = same_file;
  3440.  
  3441.   /* Expand any macros.  */
  3442.   tem = expand_to_temp_buffer (buf, limit, 0);
  3443.  
  3444.   /* Point to macroexpanded line, which is null-terminated now.  */
  3445.   bp = tem.buf;
  3446.   SKIP_WHITE_SPACE (bp);
  3447.  
  3448.   if (!isdigit (*bp)) {
  3449.     error ("invalid format #line command");
  3450.     return;
  3451.   }
  3452.  
  3453.   /* The Newline at the end of this line remains to be processed.
  3454.      To put the next line at the specified line number,
  3455.      we must store a line number now that is one less.  */
  3456.   new_lineno = atoi (bp) - 1;
  3457.  
  3458.   /* skip over the line number.  */
  3459.   while (isdigit (*bp))
  3460.     bp++;
  3461.   if (*bp && !is_space[*bp]) {
  3462.     error ("invalid format #line command");
  3463.     return;
  3464.   }
  3465.     
  3466.   SKIP_WHITE_SPACE (bp);
  3467.  
  3468.   if (*bp == '\"') {
  3469.     static HASHNODE *fname_table[FNAME_HASHSIZE];
  3470.     HASHNODE *hp, **hash_bucket;
  3471.     U_CHAR *fname;
  3472.     int fname_length;
  3473.  
  3474.     fname = ++bp;
  3475.  
  3476.     while (*bp && *bp != '\"')
  3477.       bp++;
  3478.     if (*bp != '\"') {
  3479.       error ("invalid format #line command");
  3480.       return;
  3481.     }
  3482.  
  3483.     fname_length = bp - fname;
  3484.  
  3485.     bp++;
  3486.     SKIP_WHITE_SPACE (bp);
  3487.     if (*bp) {
  3488.       if (*bp == '1')
  3489.     file_change = enter_file;
  3490.       else if (*bp == '2')
  3491.     file_change = leave_file;
  3492.       else {
  3493.     error ("invalid format #line command");
  3494.     return;
  3495.       }
  3496.  
  3497.       bp++;
  3498.       SKIP_WHITE_SPACE (bp);
  3499.       if (*bp) {
  3500.     error ("invalid format #line command");
  3501.     return;
  3502.       }
  3503.     }
  3504.  
  3505.     hash_bucket =
  3506.       &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
  3507.     for (hp = *hash_bucket; hp != NULL; hp = hp->next)
  3508.       if (hp->length == fname_length &&
  3509.       strncmp (hp->value.cpval, fname, fname_length) == 0) {
  3510.     ip->fname = hp->value.cpval;
  3511.     break;
  3512.       }
  3513.     if (hp == 0) {
  3514.       /* Didn't find it; cons up a new one.  */
  3515.       hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
  3516.       hp->next = *hash_bucket;
  3517.       *hash_bucket = hp;
  3518.  
  3519.       hp->length = fname_length;
  3520.       ip->fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
  3521.       bcopy (fname, hp->value.cpval, fname_length);
  3522.     }
  3523.   } else if (*bp) {
  3524.     error ("invalid format #line command");
  3525.     return;
  3526.   }
  3527.  
  3528.   ip->lineno = new_lineno;
  3529.   output_line_command (ip, op, 0, file_change);
  3530.   check_expand (op, ip->length - (ip->bufp - ip->buf));
  3531. }
  3532.  
  3533. /*
  3534.  * remove all definitions of symbol from symbol table.
  3535.  * according to un*x /lib/cpp, it is not an error to undef
  3536.  * something that has no definitions, so it isn't one here either.
  3537.  */
  3538. do_undef (buf, limit, op, keyword)
  3539.      U_CHAR *buf, *limit;
  3540.      FILE_BUF *op;
  3541.      struct directive *keyword;
  3542. {
  3543.   HASHNODE *hp;
  3544.  
  3545.   SKIP_WHITE_SPACE (buf);
  3546.  
  3547.   while ((hp = lookup (buf, -1, -1)) != NULL) {
  3548.     if (hp->type != T_MACRO)
  3549.       error ("undefining `%s'", hp->name);
  3550.     delete_macro (hp);
  3551.   }
  3552. }
  3553.  
  3554. /*
  3555.  * Report a fatal error detected by the program we are processing.
  3556.  * Use the text of the line in the error message, then terminate.
  3557.  * (We use error() because it prints the filename & line#.)
  3558.  */
  3559. do_error (buf, limit, op, keyword)
  3560.      U_CHAR *buf, *limit;
  3561.      FILE_BUF *op;
  3562.      struct directive *keyword;
  3563. {
  3564.   int length = limit - buf;
  3565.   char *copy = (char *) xmalloc (length + 1);
  3566.   bcopy (buf, copy, length);
  3567.   copy[length] = 0;
  3568.   SKIP_WHITE_SPACE (copy);
  3569.   error ("#error %s", copy);
  3570.   exit (FATAL_EXIT_CODE);
  3571. }
  3572.  
  3573. /* Remember the name of the current file being read from so that we can
  3574.    avoid ever including it again.  */
  3575.  
  3576. do_once ()
  3577. {
  3578.   int i;
  3579.   FILE_BUF *ip = NULL;
  3580.  
  3581.   for (i = indepth; i >= 0; i--)
  3582.     if (instack[i].fname != NULL) {
  3583.       ip = &instack[i];
  3584.       break;
  3585.     }
  3586.  
  3587.   if (ip != NULL)
  3588.     {
  3589.       struct file_name_list *new;
  3590.  
  3591.       new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  3592.       new->next = dont_repeat_files;
  3593.       dont_repeat_files = new;
  3594.       new->fname = savestring (ip->fname);
  3595.     }
  3596. }
  3597.  
  3598. /* #pragma and its argument line have already been copied to the output file.
  3599.    Here just check for recognized pragmas.  */
  3600.  
  3601. do_pragma (buf, limit)
  3602.      U_CHAR *buf, *limit;
  3603. {
  3604.   while (*buf == ' ' || *buf == '\t')
  3605.     buf++;
  3606.   if (!strncmp (buf, "once", 4))
  3607.     do_once ();
  3608. }
  3609.  
  3610. #if 0
  3611. /* This was a fun hack, but #pragma seems to start to be useful.
  3612.    By failing to recognize it, we pass it through unchanged to cc1.  */
  3613.  
  3614. /*
  3615.  * the behavior of the #pragma directive is implementation defined.
  3616.  * this implementation defines it as follows.
  3617.  */
  3618. do_pragma ()
  3619. {
  3620.   close (0);
  3621.   if (open ("/dev/tty", O_RDONLY, 0666) != 0)
  3622.     goto nope;
  3623.   close (1);
  3624.   if (open ("/dev/tty", O_WRONLY, 0666) != 1)
  3625.     goto nope;
  3626.   execl ("/usr/games/hack", "#pragma", 0);
  3627.   execl ("/usr/games/rogue", "#pragma", 0);
  3628.   execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
  3629.   execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
  3630. nope:
  3631.   fatal ("You are in a maze of twisty compiler features, all different");
  3632. }
  3633. #endif
  3634.  
  3635. /* Just ignore #sccs, on systems where we define it at all.  */
  3636. do_sccs ()
  3637. {
  3638.   if (pedantic)
  3639.     error ("ANSI C does not allow #sccs");
  3640. }
  3641.  
  3642. /*
  3643.  * handle #if command by
  3644.  *   1) inserting special `defined' keyword into the hash table
  3645.  *    that gets turned into 0 or 1 by special_symbol (thus,
  3646.  *    if the luser has a symbol called `defined' already, it won't
  3647.  *      work inside the #if command)
  3648.  *   2) rescan the input into a temporary output buffer
  3649.  *   3) pass the output buffer to the yacc parser and collect a value
  3650.  *   4) clean up the mess left from steps 1 and 2.
  3651.  *   5) call conditional_skip to skip til the next #endif (etc.),
  3652.  *      or not, depending on the value from step 3.
  3653.  */
  3654.  
  3655. do_if (buf, limit, op, keyword)
  3656.      U_CHAR *buf, *limit;
  3657.      FILE_BUF *op;
  3658.      struct directive *keyword;
  3659. {
  3660.   int value;
  3661.   FILE_BUF *ip = &instack[indepth];
  3662.  
  3663.   value = eval_if_expression (buf, limit - buf);
  3664.   conditional_skip (ip, value == 0, T_IF);
  3665. }
  3666.  
  3667. /*
  3668.  * handle a #elif directive by not changing  if_stack  either.
  3669.  * see the comment above do_else.
  3670.  */
  3671.  
  3672. do_elif (buf, limit, op, keyword)
  3673.      U_CHAR *buf, *limit;
  3674.      FILE_BUF *op;
  3675.      struct directive *keyword;
  3676. {
  3677.   int value;
  3678.   FILE_BUF *ip = &instack[indepth];
  3679.  
  3680.   if (if_stack == instack[indepth].if_stack) {
  3681.     error ("#elif not within a conditional");
  3682.     return;
  3683.   } else {
  3684.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  3685.       error ("#elif after #else");
  3686.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  3687.       if (if_stack->fname != NULL && ip->fname != NULL &&
  3688.       strcmp (if_stack->fname, ip->fname) != 0)
  3689.     fprintf (stderr, ", file %s", if_stack->fname);
  3690.       fprintf (stderr, ")\n");
  3691.     }
  3692.     if_stack->type = T_ELIF;
  3693.   }
  3694.  
  3695.   if (if_stack->if_succeeded)
  3696.     skip_if_group (ip, 0);
  3697.   else {
  3698.     value = eval_if_expression (buf, limit - buf);
  3699.     if (value == 0)
  3700.       skip_if_group (ip, 0);
  3701.     else {
  3702.       ++if_stack->if_succeeded;    /* continue processing input */
  3703.       output_line_command (ip, op, 1, same_file);
  3704.     }
  3705.   }
  3706. }
  3707.  
  3708. /*
  3709.  * evaluate a #if expression in BUF, of length LENGTH,
  3710.  * then parse the result as a C expression and return the value as an int.
  3711.  */
  3712. int
  3713. eval_if_expression (buf, length)
  3714.      U_CHAR *buf;
  3715.      int length;
  3716. {
  3717.   FILE_BUF temp_obuf;
  3718.   HASHNODE *save_defined;
  3719.   int value;
  3720.  
  3721.   save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, -1);
  3722.   temp_obuf = expand_to_temp_buffer (buf, buf + length, 0);
  3723.   delete_macro (save_defined);    /* clean up special symbol */
  3724.  
  3725.   value = parse_c_expression (temp_obuf.buf);
  3726.  
  3727.   free (temp_obuf.buf);
  3728.  
  3729.   return value;
  3730. }
  3731.  
  3732. /*
  3733.  * routine to handle ifdef/ifndef.  Try to look up the symbol,
  3734.  * then do or don't skip to the #endif/#else/#elif depending
  3735.  * on what directive is actually being processed.
  3736.  */
  3737. do_xifdef (buf, limit, op, keyword)
  3738.      U_CHAR *buf, *limit;
  3739.      FILE_BUF *op;
  3740.      struct directive *keyword;
  3741. {
  3742.   int skip;
  3743.   FILE_BUF *ip = &instack[indepth];
  3744.   U_CHAR *end; 
  3745.  
  3746.   /* Discard leading and trailing whitespace.  */
  3747.   SKIP_WHITE_SPACE (buf);
  3748.   while (limit != buf && is_hor_space[limit[-1]]) limit--;
  3749.  
  3750.   /* Find the end of the identifier at the beginning.  */
  3751.   for (end = buf; is_idchar[*end]; end++);
  3752.  
  3753.   if (end == buf) {
  3754.     skip = (keyword->type == T_IFDEF);
  3755.     if (! traditional)
  3756.       warning (end == limit ? "#%s with no argument"
  3757.            : "#%s argument starts with punctuation",
  3758.            keyword->name);
  3759.   } else {
  3760.     if (pedantic && buf[0] >= '0' && buf[0] <= '9')
  3761.       warning ("#%s argument starts with a digit", keyword->name);
  3762.     else if (end != limit && !traditional)
  3763.       warning ("garbage at end of #%s argument", keyword->name);
  3764.  
  3765.     skip = (lookup (buf, end-buf, -1) == NULL) ^ (keyword->type == T_IFNDEF);
  3766.   }
  3767.  
  3768.   conditional_skip (ip, skip, T_IF);
  3769. }
  3770.  
  3771. /*
  3772.  * push TYPE on stack; then, if SKIP is nonzero, skip ahead.
  3773.  */
  3774. void
  3775. conditional_skip (ip, skip, type)
  3776.      FILE_BUF *ip;
  3777.      int skip;
  3778.      enum node_type type;
  3779. {
  3780.   IF_STACK_FRAME *temp;
  3781.  
  3782.   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  3783.   temp->fname = ip->fname;
  3784.   temp->lineno = ip->lineno;
  3785.   temp->next = if_stack;
  3786.   if_stack = temp;
  3787.  
  3788.   if_stack->type = type;
  3789.  
  3790.   if (skip != 0) {
  3791.     skip_if_group (ip, 0);
  3792.     return;
  3793.   } else {
  3794.     ++if_stack->if_succeeded;
  3795.     output_line_command (ip, &outbuf, 1, same_file);
  3796.   }
  3797. }
  3798.  
  3799. /*
  3800.  * skip to #endif, #else, or #elif.  adjust line numbers, etc.
  3801.  * leaves input ptr at the sharp sign found.
  3802.  * If ANY is nonzero, return at next directive of any sort.
  3803.  */
  3804. void
  3805. skip_if_group (ip, any)
  3806.      FILE_BUF *ip;
  3807.      int any;
  3808. {
  3809.   register U_CHAR *bp = ip->bufp, *cp;
  3810.   register U_CHAR *endb = ip->buf + ip->length;
  3811.   struct directive *kt;
  3812.   IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
  3813.   U_CHAR *beg_of_line = bp;
  3814.  
  3815.   while (bp < endb) {
  3816.     switch (*bp++) {
  3817.     case '/':            /* possible comment */
  3818.       if (*bp == '\\' && bp[1] == '\n')
  3819.     newline_fix (bp);
  3820.       if (*bp == '*'
  3821.       || (cplusplus && *bp == '/')) {
  3822.     ip->bufp = ++bp;
  3823.     bp = skip_to_end_of_comment (ip, &ip->lineno);
  3824.       }
  3825.       break;
  3826.     case '\"':
  3827.     case '\'':
  3828.       if (!traditional)
  3829.     bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno, 0, 0);
  3830.       break;
  3831.     case '\\':
  3832.       /* Char after backslash loses its special meaning.  */
  3833.       if (bp < endb) {
  3834.     if (*bp == '\n')
  3835.       ++ip->lineno;        /* But do update the line-count.  */
  3836.     bp++;
  3837.       }
  3838.       break;
  3839.     case '\n':
  3840.       ++ip->lineno;
  3841.       beg_of_line = bp;
  3842.       break;
  3843.     case '#':
  3844.       ip->bufp = bp - 1;
  3845.  
  3846.       /* # keyword: a # must be first nonblank char on the line */
  3847.       if (beg_of_line == 0)
  3848.     break;
  3849.       /* Scan from start of line, skipping whitespace, comments
  3850.      and backslash-newlines, and see if we reach this #.
  3851.      If not, this # is not special.  */
  3852.       bp = beg_of_line;
  3853.       while (1) {
  3854.     if (is_hor_space[*bp])
  3855.       bp++;
  3856.     else if (*bp == '\\' && bp[1] == '\n')
  3857.       bp += 2;
  3858.     else if (*bp == '/' && bp[1] == '*') {
  3859.       bp += 2;
  3860.       while (!(*bp == '*' && bp[1] == '/'))
  3861.         bp++;
  3862.       bp += 2;
  3863.     }
  3864.     else if (cplusplus && *bp == '/' && bp[1] == '/') {
  3865.       bp += 2;
  3866.       while (*bp++ != '\n') ;
  3867.         }
  3868.     else break;
  3869.       }
  3870.       if (bp != ip->bufp) {
  3871.     bp = ip->bufp + 1;    /* Reset bp to after the #.  */
  3872.     break;
  3873.       }
  3874.  
  3875.       bp = ip->bufp + 1;        /* point at '#' */
  3876.  
  3877.       /* Skip whitespace and \-newline.  */
  3878.       while (1) {
  3879.     if (is_hor_space[*bp])
  3880.       bp++;
  3881.     else if (*bp == '\\' && bp[1] == '\n')
  3882.       bp += 2;
  3883.     else break;
  3884.       }
  3885.  
  3886.       cp = bp;
  3887.  
  3888.       /* Now find end of directive name.
  3889.      If we encounter a backslash-newline, exchange it with any following
  3890.      symbol-constituents so that we end up with a contiguous name.  */
  3891.  
  3892.       while (1) {
  3893.     if (is_idchar[*bp])
  3894.       bp++;
  3895.     else {
  3896.       if (*bp == '\\' && bp[1] == '\n')
  3897.         name_newline_fix (bp);
  3898.       if (is_idchar[*bp])
  3899.         bp++;
  3900.       else break;
  3901.     }
  3902.       }
  3903.  
  3904.       for (kt = directive_table; kt->length >= 0; kt++) {
  3905.     IF_STACK_FRAME *temp;
  3906.     if (strncmp (cp, kt->name, kt->length) == 0
  3907.         && !is_idchar[cp[kt->length]]) {
  3908.  
  3909.       /* If we are asked to return on next directive,
  3910.          do so now.  */
  3911.       if (any)
  3912.         return;
  3913.  
  3914.       switch (kt->type) {
  3915.       case T_IF:
  3916.       case T_IFDEF:
  3917.       case T_IFNDEF:
  3918.         temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  3919.         temp->next = if_stack;
  3920.         if_stack = temp;
  3921.         temp->lineno = ip->lineno;
  3922.         temp->fname = ip->fname;
  3923.         temp->type = kt->type;
  3924.         break;
  3925.       case T_ELSE:
  3926.       case T_ENDIF:
  3927.         if (pedantic && if_stack != save_if_stack)
  3928.           validate_else (bp);
  3929.       case T_ELIF:
  3930.         if (if_stack == instack[indepth].if_stack) {
  3931.           error ("#%s not within a conditional", kt->name);
  3932.           break;
  3933.         }
  3934.         else if (if_stack == save_if_stack)
  3935.           return;        /* found what we came for */
  3936.  
  3937.         if (kt->type != T_ENDIF) {
  3938.           if (if_stack->type == T_ELSE)
  3939.         error ("#else or #elif after #else");
  3940.           if_stack->type = kt->type;
  3941.           break;
  3942.         }
  3943.  
  3944.         temp = if_stack;
  3945.         if_stack = if_stack->next;
  3946.         free (temp);
  3947.         break;
  3948.       }
  3949.       break;
  3950.     }
  3951.       }
  3952.     }
  3953.   }
  3954.   ip->bufp = bp;
  3955.   /* after this returns, rescan will exit because ip->bufp
  3956.      now points to the end of the buffer.
  3957.      rescan is responsible for the error message also.  */
  3958. }
  3959.  
  3960. /*
  3961.  * handle a #else directive.  Do this by just continuing processing
  3962.  * without changing  if_stack ;  this is so that the error message
  3963.  * for missing #endif's etc. will point to the original #if.  It
  3964.  * is possible that something different would be better.
  3965.  */
  3966. do_else (buf, limit, op, keyword)
  3967.      U_CHAR *buf, *limit;
  3968.      FILE_BUF *op;
  3969.      struct directive *keyword;
  3970. {
  3971.   FILE_BUF *ip = &instack[indepth];
  3972.  
  3973.   if (pedantic) {
  3974.     SKIP_WHITE_SPACE (buf);
  3975.     if (buf != limit)
  3976.       warning ("text following #else violates ANSI standard");
  3977.   }
  3978.  
  3979.   if (if_stack == instack[indepth].if_stack) {
  3980.     error ("#else not within a conditional");
  3981.     return;
  3982.   } else {
  3983.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  3984.       error ("#else after #else");
  3985.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  3986.       if (strcmp (if_stack->fname, ip->fname) != 0)
  3987.     fprintf (stderr, ", file %s", if_stack->fname);
  3988.       fprintf (stderr, ")\n");
  3989.     }
  3990.     if_stack->type = T_ELSE;
  3991.   }
  3992.  
  3993.   if (if_stack->if_succeeded)
  3994.     skip_if_group (ip, 0);
  3995.   else {
  3996.     ++if_stack->if_succeeded;    /* continue processing input */
  3997.     output_line_command (ip, op, 1, same_file);
  3998.   }
  3999. }
  4000.  
  4001. /*
  4002.  * unstack after #endif command
  4003.  */
  4004. do_endif (buf, limit, op, keyword)
  4005.      U_CHAR *buf, *limit;
  4006.      FILE_BUF *op;
  4007.      struct directive *keyword;
  4008. {
  4009.   if (pedantic) {
  4010.     SKIP_WHITE_SPACE (buf);
  4011.     if (buf != limit)
  4012.       warning ("text following #endif violates ANSI standard");
  4013.   }
  4014.  
  4015.   if (if_stack == instack[indepth].if_stack)
  4016.     error ("unbalanced #endif");
  4017.   else {
  4018.     IF_STACK_FRAME *temp = if_stack;
  4019.     if_stack = if_stack->next;
  4020.     free (temp);
  4021.     output_line_command (&instack[indepth], op, 1, same_file);
  4022.   }
  4023. }
  4024.  
  4025. /* When an #else or #endif is found while skipping failed conditional,
  4026.    if -pedantic was specified, this is called to warn about text after
  4027.    the command name.  P points to the first char after the command name.  */
  4028.  
  4029. validate_else (p)
  4030.      register U_CHAR *p;
  4031. {
  4032.   /* Advance P over whitespace and comments.  */
  4033.   while (1) {
  4034.     if (*p == '\\' && p[1] == '\n')
  4035.       p += 2;
  4036.     if (is_hor_space[*p])
  4037.       p++;
  4038.     else if (*p == '/') {
  4039.       if (p[1] == '\\' && p[2] == '\n')
  4040.     newline_fix (p + 1);
  4041.       if (p[1] == '*') {
  4042.     p += 2;
  4043.     /* Don't bother warning about unterminated comments
  4044.        since that will happen later.  Just be sure to exit.  */
  4045.     while (*p) {
  4046.       if (p[1] == '\\' && p[2] == '\n')
  4047.         newline_fix (p + 1);
  4048.       if (*p == '*' && p[1] == '/') {
  4049.         p += 2;
  4050.         break;
  4051.       }
  4052.       p++;
  4053.     }
  4054.       }
  4055.       else if (cplusplus && p[1] == '/') {
  4056.     p += 2;
  4057.     while (*p && *p++ != '\n') ;
  4058.       }
  4059.     } else break;
  4060.   }
  4061.   if (*p && *p != '\n')
  4062.     warning ("text following #else or #endif violates ANSI standard");
  4063. }
  4064.  
  4065. /*
  4066.  * Skip a comment, assuming the input ptr immediately follows the
  4067.  * initial slash-star.  Bump line counter as necessary.
  4068.  * (The canonical line counter is &ip->lineno).
  4069.  * Don't use this routine (or the next one) if bumping the line
  4070.  * counter is not sufficient to deal with newlines in the string.
  4071.  */
  4072. U_CHAR *
  4073. skip_to_end_of_comment (ip, line_counter)
  4074.      register FILE_BUF *ip;
  4075.      int *line_counter;        /* place to remember newlines, or NULL */
  4076. {
  4077.   register U_CHAR *limit = ip->buf + ip->length;
  4078.   register U_CHAR *bp = ip->bufp;
  4079.   FILE_BUF *op = &outbuf;    /* JF */
  4080.   int output = put_out_comments && !line_counter;
  4081.  
  4082.     /* JF this line_counter stuff is a crock to make sure the
  4083.        comment is only put out once, no matter how many times
  4084.        the comment is skipped.  It almost works */
  4085.   if (output) {
  4086.     *op->bufp++ = '/';
  4087.     *op->bufp++ = '*';
  4088.   }
  4089.   if (cplusplus && bp[-1] == '/') {
  4090.     if (output) {
  4091.       while (bp < limit)
  4092.     if ((*op->bufp++ = *bp++) == '\n') {
  4093.       bp--;
  4094.       break;
  4095.     }
  4096.       op->bufp[-1] = '*';
  4097.       *op->bufp++ = '/';
  4098.       *op->bufp++ = '\n';
  4099.     } else {
  4100.       while (bp < limit) {
  4101.     if (*bp++ == '\n') {
  4102.       bp--;
  4103.       break;
  4104.     }
  4105.       }
  4106.     }
  4107.     ip->bufp = bp;
  4108.     return bp;
  4109.   }
  4110.   while (bp < limit) {
  4111.     if (output)
  4112.       *op->bufp++ = *bp;
  4113.     switch (*bp++) {
  4114.     case '\n':
  4115.       if (line_counter != NULL)
  4116.     ++*line_counter;
  4117.       if (output)
  4118.     ++op->lineno;
  4119.       break;
  4120.     case '*':
  4121.       if (*bp == '\\' && bp[1] == '\n')
  4122.     newline_fix (bp);
  4123.       if (*bp == '/') {
  4124.         if (output)
  4125.       *op->bufp++ = '/';
  4126.     ip->bufp = ++bp;
  4127.     return bp;
  4128.       }
  4129.       break;
  4130.     }
  4131.   }
  4132.   ip->bufp = bp;
  4133.   return bp;
  4134. }
  4135.  
  4136. /*
  4137.  * Skip over a quoted string.  BP points to the opening quote.
  4138.  * Returns a pointer after the closing quote.  Don't go past LIMIT.
  4139.  * START_LINE is the line number of the starting point (but it need
  4140.  * not be valid if the starting point is inside a macro expansion).
  4141.  *
  4142.  * The input stack state is not changed.
  4143.  *
  4144.  * If COUNT_NEWLINES is nonzero, it points to an int to increment
  4145.  * for each newline passed.
  4146.  *
  4147.  * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
  4148.  * if we pass a backslash-newline.
  4149.  *
  4150.  * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
  4151.  */
  4152. U_CHAR *
  4153. skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
  4154.      register U_CHAR *bp;
  4155.      register U_CHAR *limit;
  4156.      int start_line;
  4157.      int *count_newlines;
  4158.      int *backslash_newlines_p;
  4159.      int *eofp;
  4160. {
  4161.   register U_CHAR c, match;
  4162.  
  4163.   match = *bp++;
  4164.   while (1) {
  4165.     if (bp >= limit) {
  4166.       error_with_line (line_for_error (start_line),
  4167.                "unterminated string or character constant");
  4168.       if (eofp)
  4169.     *eofp = 1;
  4170.       break;
  4171.     }
  4172.     c = *bp++;
  4173.     if (c == '\\') {
  4174.       while (*bp == '\\' && bp[1] == '\n') {
  4175.     if (backslash_newlines_p)
  4176.       *backslash_newlines_p = 1;
  4177.     if (count_newlines)
  4178.       ++*count_newlines;
  4179.     bp += 2;
  4180.       }
  4181.       if (*bp == '\n' && count_newlines) {
  4182.     if (backslash_newlines_p)
  4183.       *backslash_newlines_p = 1;
  4184.     ++*count_newlines;
  4185.       }
  4186.       bp++;
  4187.     } else if (c == '\n') {
  4188.       if (match == '\'') {
  4189.     error_with_line (line_for_error (start_line),
  4190.              "unterminated character constant");
  4191.     bp--;
  4192.     if (eofp)
  4193.       *eofp = 1;
  4194.     break;
  4195.       }
  4196.       if (traditional) {    /* Unterminated strings are 'legal'.  */
  4197.     if (eofp)
  4198.       *eofp = 1;
  4199.     break;
  4200.       }
  4201.       if (count_newlines)
  4202.     ++*count_newlines;
  4203.     } else if (c == match)
  4204.       break;
  4205.   }
  4206.   return bp;
  4207. }
  4208.  
  4209. /*
  4210.  * write out a #line command, for instance, after an #include file.
  4211.  * If CONDITIONAL is nonzero, we can omit the #line if it would
  4212.  * appear to be a no-op, and we can output a few newlines instead
  4213.  * if we want to increase the line number by a small amount.
  4214.  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
  4215.  */
  4216.  
  4217. void
  4218. output_line_command (ip, op, conditional, file_change)
  4219.      FILE_BUF *ip, *op;
  4220.      int conditional;
  4221.      enum file_change_code file_change;
  4222. {
  4223.   int len;
  4224.   char line_cmd_buf[500];
  4225.  
  4226.   if (no_line_commands
  4227.       || ip->fname == NULL
  4228.       || no_output) {
  4229.     op->lineno = ip->lineno;
  4230.     return;
  4231.   }
  4232.  
  4233.   if (conditional) {
  4234.     if (ip->lineno == op->lineno)
  4235.       return;
  4236.  
  4237.     /* If the inherited line number is a little too small,
  4238.        output some newlines instead of a #line command.  */
  4239.     if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
  4240.       check_expand (op, 10);
  4241.       while (ip->lineno > op->lineno) {
  4242.     *op->bufp++ = '\n';
  4243.     op->lineno++;
  4244.       }
  4245.       return;
  4246.     }
  4247.   }
  4248.  
  4249. #ifdef OUTPUT_LINE_COMMANDS
  4250.   sprintf (line_cmd_buf, "#line %d \"%s\"", ip->lineno, ip->fname);
  4251. #else
  4252.   sprintf (line_cmd_buf, "# %d \"%s\"", ip->lineno, ip->fname);
  4253. #endif
  4254.   if (file_change != same_file)
  4255.     strcat (line_cmd_buf, file_change == enter_file ? " 1" : " 2");
  4256.   len = strlen (line_cmd_buf);
  4257.   line_cmd_buf[len++] = '\n';
  4258.   check_expand (op, len + 1);
  4259.   if (op->bufp > op->buf && op->bufp[-1] != '\n')
  4260.     *op->bufp++ = '\n';
  4261. #ifdef atarist
  4262.   op->bufp = slashifying_bcopy (line_cmd_buf, op->bufp, len);
  4263. #else
  4264.   bcopy (line_cmd_buf, op->bufp, len);
  4265.   op->bufp += len;
  4266. #endif
  4267.   op->lineno = ip->lineno;
  4268. }
  4269.  
  4270. /* This structure represents one parsed argument in a macro call.
  4271.    `raw' points to the argument text as written (`raw_length' is its length).
  4272.    `expanded' points to the argument's macro-expansion
  4273.    (its length is `expand_length').
  4274.    `stringified_length' is the length the argument would have
  4275.    if stringified.
  4276.    `free1' and `free2', if nonzero, point to blocks to be freed
  4277.    when the macro argument data is no longer needed.  */
  4278.  
  4279. struct argdata {
  4280.   U_CHAR *raw, *expanded;
  4281.   int raw_length, expand_length;
  4282.   int stringified_length;
  4283.   U_CHAR *free1, *free2;
  4284.   char newlines;
  4285.   char comments;
  4286. };
  4287.  
  4288. /* Expand a macro call.
  4289.    HP points to the symbol that is the macro being called.
  4290.    Put the result of expansion onto the input stack
  4291.    so that subsequent input by our caller will use it.
  4292.  
  4293.    If macro wants arguments, caller has already verified that
  4294.    an argument list follows; arguments come from the input stack.  */
  4295.  
  4296. void
  4297. macroexpand (hp, op)
  4298.      HASHNODE *hp;
  4299.      FILE_BUF *op;
  4300. {
  4301.   int nargs;
  4302.   DEFINITION *defn = hp->value.defn;
  4303.   register U_CHAR *xbuf;
  4304.   int xbuf_len;
  4305.   int start_line = instack[indepth].lineno;
  4306.  
  4307.   CHECK_DEPTH (return;);
  4308.  
  4309.   /* it might not actually be a macro.  */
  4310.   if (hp->type != T_MACRO) {
  4311.     special_symbol (hp, op);
  4312.     return;
  4313.   }
  4314.  
  4315.   nargs = defn->nargs;
  4316.  
  4317.   if (nargs >= 0) {
  4318.     register int i;
  4319.     struct argdata *args;
  4320.     char *parse_error = 0;
  4321.  
  4322.     args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
  4323.  
  4324.     for (i = 0; i < nargs; i++) {
  4325.       args[i].raw = args[i].expanded = (U_CHAR *) "";
  4326.       args[i].raw_length = args[i].expand_length
  4327.     = args[i].stringified_length = 0;
  4328.       args[i].free1 = args[i].free2 = 0;
  4329.     }
  4330.  
  4331.     /* Parse all the macro args that are supplied.  I counts them.
  4332.        The first NARGS args are stored in ARGS.
  4333.        The rest are discarded.  */
  4334.     i = 0;
  4335.     do {
  4336.       /* Discard the open-parenthesis or comma before the next arg.  */
  4337.       ++instack[indepth].bufp;
  4338.       parse_error
  4339.     = macarg ((i < nargs || (nargs == 0 && i == 0)) ? &args[i] : 0);
  4340.       if (parse_error)
  4341.     {
  4342.       error_with_line (line_for_error (start_line), parse_error);
  4343.       break;
  4344.     }
  4345.       i++;
  4346.     } while (*instack[indepth].bufp != ')');
  4347.  
  4348.     /* If we got one arg but it was just whitespace, call that 0 args.  */
  4349.     if (i == 1) {
  4350.       register U_CHAR *bp = args[0].raw;
  4351.       register U_CHAR *lim = bp + args[0].raw_length;
  4352.       while (bp != lim && is_space[*bp]) bp++;
  4353.       if (bp == lim)
  4354.     i = 0;
  4355.     }
  4356.  
  4357.     if (nargs == 0 && i > 0)
  4358.       error ("arguments given to macro `%s'", hp->name);
  4359.     else if (i < nargs) {
  4360.       /* traditional C allows foo() if foo wants one argument.  */
  4361.       if (nargs == 1 && i == 0 && traditional)
  4362.     ;
  4363.       else if (i == 0)
  4364.     error ("no args to macro `%s'", hp->name);
  4365.       else if (i == 1)
  4366.     error ("only 1 arg to macro `%s'", hp->name);
  4367.       else
  4368.     error ("only %d args to macro `%s'", i, hp->name);
  4369.     } else if (i > nargs)
  4370.       error ("too many (%d) args to macro `%s'", i, hp->name);
  4371.  
  4372.     /* Swallow the closeparen.  */
  4373.     ++instack[indepth].bufp;
  4374.  
  4375.     /* If macro wants zero args, we parsed the arglist for checking only.
  4376.        Read directly from the macro definition.  */
  4377.     if (nargs == 0) {
  4378.       xbuf = defn->expansion;
  4379.       xbuf_len = defn->length;
  4380.     } else {
  4381.       register U_CHAR *exp = defn->expansion;
  4382.       register int offset;    /* offset in expansion,
  4383.                    copied a piece at a time */
  4384.       register int totlen;    /* total amount of exp buffer filled so far */
  4385.  
  4386.       register struct reflist *ap;
  4387.  
  4388.       /* Macro really takes args.  Compute the expansion of this call.  */
  4389.  
  4390.       /* Compute length in characters of the macro's expansion.  */
  4391.       xbuf_len = defn->length;
  4392.       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  4393.     if (ap->stringify)
  4394.       xbuf_len += args[ap->argno].stringified_length;
  4395.     else if (ap->raw_before || ap->raw_after)
  4396.       xbuf_len += args[ap->argno].raw_length;
  4397.     else
  4398.       xbuf_len += args[ap->argno].expand_length;
  4399.       }
  4400.  
  4401.       xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
  4402.  
  4403.       /* Generate in XBUF the complete expansion
  4404.      with arguments substituted in.
  4405.      TOTLEN is the total size generated so far.
  4406.      OFFSET is the index in the definition
  4407.      of where we are copying from.  */
  4408.       offset = totlen = 0;
  4409.       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  4410.     register struct argdata *arg = &args[ap->argno];
  4411.  
  4412.     for (i = 0; i < ap->nchars; i++)
  4413.       xbuf[totlen++] = exp[offset++];
  4414.  
  4415.     if (ap->stringify != 0) {
  4416.       int arglen = arg->raw_length;
  4417.       int escaped = 0;
  4418.       int in_string = 0;
  4419.       int c;
  4420.       i = 0;
  4421.       while (i < arglen
  4422.          && (c = arg->raw[i], is_space[c]))
  4423.         i++;
  4424.       while (i < arglen
  4425.          && (c = arg->raw[arglen - 1], is_space[c]))
  4426.         arglen--;
  4427.       if (!traditional)
  4428.         xbuf[totlen++] = '\"'; /* insert beginning quote */
  4429.       for (; i < arglen; i++) {
  4430.         c = arg->raw[i];
  4431.  
  4432.         /* Special markers Newline Space
  4433.            generate nothing for a stringified argument.  */
  4434.         if (c == '\n' && arg->raw[i+1] != '\n') {
  4435.           i++;
  4436.           continue;
  4437.         }
  4438.  
  4439.         /* Internal sequences of whitespace are replaced by one space.  */
  4440.         if (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c]) {
  4441.           while (1) {
  4442.         if (c == '\n' && arg->raw[i+1] == '\n')
  4443.           i += 2;
  4444.         else if (c != '\n' && is_space[c])
  4445.           i++;
  4446.         else break;
  4447.         c = arg->raw[i];
  4448.           }
  4449.           i--;
  4450.           c = ' ';
  4451.         }
  4452.  
  4453.         if (escaped)
  4454.           escaped = 0;
  4455.         else {
  4456.           if (c == '\\')
  4457.         escaped = 1;
  4458.           if (in_string && c == in_string)
  4459.         in_string = 0;
  4460.           else if (c == '\"' || c == '\'')
  4461.         in_string = c;
  4462.         }
  4463.  
  4464.         /* Escape these chars */
  4465.         if (c == '\"' || (in_string && c == '\\'))
  4466.           xbuf[totlen++] = '\\';
  4467.         if (isprint (c))
  4468.           xbuf[totlen++] = c;
  4469.         else {
  4470.           sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
  4471.           totlen += 4;
  4472.         }
  4473.       }
  4474.       if (!traditional)
  4475.         xbuf[totlen++] = '\"'; /* insert ending quote */
  4476.     } else if (ap->raw_before || ap->raw_after) {
  4477.       U_CHAR *p1 = arg->raw;
  4478.       U_CHAR *l1 = p1 + arg->raw_length;
  4479.       if (ap->raw_before) {
  4480.         while (p1 != l1 && is_space[*p1]) p1++;
  4481.         while (p1 != l1 && is_idchar[*p1])
  4482.           xbuf[totlen++] = *p1++;
  4483.         /* Delete any no-reexpansion marker that follows
  4484.            an identifier at the beginning of the argument
  4485.            if the argument is concatenated with what precedes it.  */
  4486.         if (p1[0] == '\n' && p1[1] == '-')
  4487.           p1 += 2;
  4488.       }
  4489.       if (ap->raw_after) {
  4490.         /* Arg is concatenated after: delete trailing whitespace,
  4491.            whitespace markers, and no-reexpansion markers.  */
  4492.         while (p1 != l1) {
  4493.           if (is_space[l1[-1]]) l1--;
  4494.           else if (l1[-1] == '-') {
  4495.         U_CHAR *p2 = l1 - 1;
  4496.         /* If a `-' is preceded by an odd number of newlines then it
  4497.            and the last newline are a no-reexpansion marker.  */
  4498.         while (p2 != p1 && p2[-1] == '\n') p2--;
  4499.         if ((l1 - 1 - p2) & 1) {
  4500.           l1 -= 2;
  4501.         }
  4502.         else break;
  4503.           }
  4504.           else break;
  4505.         }
  4506.       }
  4507.       bcopy (p1, xbuf + totlen, l1 - p1);
  4508.       totlen += l1 - p1;
  4509.     } else {
  4510.       bcopy (arg->expanded, xbuf + totlen, arg->expand_length);
  4511.       totlen += arg->expand_length;
  4512.     }
  4513.  
  4514.     if (totlen > xbuf_len)
  4515.       abort ();
  4516.       }
  4517.  
  4518.       /* if there is anything left of the definition
  4519.      after handling the arg list, copy that in too. */
  4520.  
  4521.       for (i = offset; i < defn->length; i++)
  4522.     xbuf[totlen++] = exp[i];
  4523.  
  4524.       xbuf[totlen] = 0;
  4525.       xbuf_len = totlen;
  4526.  
  4527.       for (i = 0; i < nargs; i++) {
  4528.     if (args[i].free1 != 0)
  4529.       free (args[i].free1);
  4530.     if (args[i].free2 != 0)
  4531.       free (args[i].free2);
  4532.       }
  4533.     }
  4534.   } else {
  4535.     xbuf = defn->expansion;
  4536.     xbuf_len = defn->length;
  4537.   }
  4538.  
  4539.   /* Now put the expansion on the input stack
  4540.      so our caller will commence reading from it.  */
  4541.   {
  4542.     register FILE_BUF *ip2;
  4543.  
  4544.     ip2 = &instack[++indepth];
  4545.  
  4546.     ip2->fname = 0;
  4547.     ip2->lineno = 0;
  4548.     ip2->buf = xbuf;
  4549.     ip2->length = xbuf_len;
  4550.     ip2->bufp = xbuf;
  4551.     ip2->free_ptr = (nargs > 0) ? xbuf : 0;
  4552.     ip2->macro = hp;
  4553.     ip2->if_stack = if_stack;
  4554.  
  4555.     /* Recursive macro use sometimes works traditionally.
  4556.        #define foo(x,y) bar(x(y,0), y)
  4557.        foo(foo, baz)  */
  4558.  
  4559.     if (!traditional)
  4560.       hp->type = T_DISABLED;
  4561.   }
  4562. }
  4563.  
  4564. /*
  4565.  * Parse a macro argument and store the info on it into *ARGPTR.
  4566.  * Return nonzero to indicate a syntax error.
  4567.  */
  4568.  
  4569. char *
  4570. macarg (argptr)
  4571.      register struct argdata *argptr;
  4572. {
  4573.   FILE_BUF *ip = &instack[indepth];
  4574.   int paren = 0;
  4575.   int newlines = 0;
  4576.   int comments = 0;
  4577.  
  4578.   /* Try to parse as much of the argument as exists at this
  4579.      input stack level.  */
  4580.   U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
  4581.             &paren, &newlines, &comments);
  4582.  
  4583.   /* If we find the end of the argument at this level,
  4584.      set up *ARGPTR to point at it in the input stack.  */
  4585.   if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
  4586.       && bp != ip->buf + ip->length) {
  4587.     if (argptr != 0) {
  4588.       argptr->raw = ip->bufp;
  4589.       argptr->raw_length = bp - ip->bufp;
  4590.     }
  4591.     ip->bufp = bp;
  4592.   } else {
  4593.     /* This input stack level ends before the macro argument does.
  4594.        We must pop levels and keep parsing.
  4595.        Therefore, we must allocate a temporary buffer and copy
  4596.        the macro argument into it.  */
  4597.     int bufsize = bp - ip->bufp;
  4598.     int extra = newlines;
  4599.     U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
  4600.     int final_start = 0;
  4601.  
  4602.     bcopy (ip->bufp, buffer, bufsize);
  4603.     ip->bufp = bp;
  4604.     ip->lineno += newlines;
  4605.  
  4606.     while (bp == ip->buf + ip->length) {
  4607.       if (instack[indepth].macro == 0) {
  4608.     free (buffer);
  4609.     return "unterminated macro call";
  4610.       }
  4611.       ip->macro->type = T_MACRO;
  4612.       free (ip->buf);
  4613.       ip = &instack[--indepth];
  4614.       newlines = 0;
  4615.       comments = 0;
  4616.       bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
  4617.             &newlines, &comments);
  4618.       final_start = bufsize;
  4619.       bufsize += bp - ip->bufp;
  4620.       extra += newlines;
  4621.       buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
  4622.       bcopy (ip->bufp, buffer + bufsize - (bp - ip->bufp), bp - ip->bufp);
  4623.       ip->bufp = bp;
  4624.       ip->lineno += newlines;
  4625.     }
  4626.  
  4627.     /* Now, if arg is actually wanted, record its raw form,
  4628.        discarding comments and duplicating newlines in whatever
  4629.        part of it did not come from a macro expansion.
  4630.        EXTRA space has been preallocated for duplicating the newlines.
  4631.        FINAL_START is the index of the start of that part.  */
  4632.     if (argptr != 0) {
  4633.       argptr->raw = buffer;
  4634.       argptr->raw_length = bufsize;
  4635.       argptr->free1 = buffer;
  4636.       argptr->newlines = newlines;
  4637.       argptr->comments = comments;
  4638.       if ((newlines || comments) && ip->fname != 0)
  4639.     argptr->raw_length
  4640.       = final_start +
  4641.         discard_comments (argptr->raw + final_start,
  4642.                   argptr->raw_length - final_start,
  4643.                   newlines);
  4644.       argptr->raw[argptr->raw_length] = 0;
  4645.       if (argptr->raw_length > bufsize + extra)
  4646.     abort ();
  4647.     }
  4648.   }
  4649.  
  4650.   /* If we are not discarding this argument,
  4651.      macroexpand it and compute its length as stringified.
  4652.      All this info goes into *ARGPTR.  */
  4653.  
  4654.   if (argptr != 0) {
  4655.     FILE_BUF obuf;
  4656.     register U_CHAR *buf, *lim;
  4657.     register int totlen;
  4658.  
  4659.     obuf = expand_to_temp_buffer (argptr->raw,
  4660.                   argptr->raw + argptr->raw_length,
  4661.                   1);
  4662.  
  4663.     argptr->expanded = obuf.buf;
  4664.     argptr->expand_length = obuf.length;
  4665.     argptr->free2 = obuf.buf;
  4666.  
  4667.     buf = argptr->raw;
  4668.     lim = buf + argptr->raw_length;
  4669.  
  4670.     while (buf != lim && is_space[*buf])
  4671.       buf++;
  4672.     while (buf != lim && is_space[lim[-1]])
  4673.       lim--;
  4674.     totlen = traditional ? 0 : 2;    /* Count opening and closing quote.  */
  4675.     while (buf != lim) {
  4676.       register U_CHAR c = *buf++;
  4677.       totlen++;
  4678.       /* Internal sequences of whitespace are replaced by one space.  */
  4679.       if (is_space[c])
  4680.     SKIP_ALL_WHITE_SPACE (buf);
  4681.       else if (c == '\"' || c == '\\') /* escape these chars */
  4682.     totlen++;
  4683.       else if (!isprint (c))
  4684.     totlen += 3;
  4685.     }
  4686.     argptr->stringified_length = totlen;
  4687.   }
  4688.   return 0;
  4689. }
  4690.  
  4691. /* Scan text from START (inclusive) up to LIMIT (exclusive),
  4692.    counting parens in *DEPTHPTR,
  4693.    and return if reach LIMIT
  4694.    or before a `)' that would make *DEPTHPTR negative
  4695.    or before a comma when *DEPTHPTR is zero.
  4696.    Single and double quotes are matched and termination
  4697.    is inhibited within them.  Comments also inhibit it.
  4698.    Value returned is pointer to stopping place.
  4699.  
  4700.    Increment *NEWLINES each time a newline is passed.
  4701.    Set *COMMENTS to 1 if a comment is seen.  */
  4702.  
  4703. U_CHAR *
  4704. macarg1 (start, limit, depthptr, newlines, comments)
  4705.      U_CHAR *start;
  4706.      register U_CHAR *limit;
  4707.      int *depthptr, *newlines, *comments;
  4708. {
  4709.   register U_CHAR *bp = start;
  4710.  
  4711.   while (bp < limit) {
  4712.     switch (*bp) {
  4713.     case '(':
  4714.       (*depthptr)++;
  4715.       break;
  4716.     case ')':
  4717.       if (--(*depthptr) < 0)
  4718.     return bp;
  4719.       break;
  4720.     case '\\':
  4721.       /* Backslash makes following char not special.  */
  4722.       if (bp + 1 < limit) bp++;
  4723.       break;
  4724.     case '\n':
  4725.       ++*newlines;
  4726.       break;
  4727.     case '/':
  4728.       if (bp[1] == '\\' && bp[2] == '\n')
  4729.     newline_fix (bp + 1);
  4730.       if (cplusplus && bp[1] == '/') {
  4731.     *comments = 1;
  4732.     bp += 2;
  4733.     while (bp < limit && *bp++ != '\n') ;
  4734.     ++*newlines;
  4735.     break;
  4736.       }
  4737.       if (bp[1] != '*' || bp + 1 >= limit)
  4738.     break;
  4739.       *comments = 1;
  4740.       bp += 2;
  4741.       while (bp + 1 < limit) {
  4742.     if (bp[0] == '*'
  4743.         && bp[1] == '\\' && bp[2] == '\n')
  4744.       newline_fix (bp + 1);
  4745.     if (bp[0] == '*' && bp[1] == '/')
  4746.       break;
  4747.     if (*bp == '\n') ++*newlines;
  4748.     bp++;
  4749.       }
  4750.       break;
  4751.     case '\'':
  4752.     case '\"':
  4753.       {
  4754.     int quotec;
  4755.     for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
  4756.       if (*bp == '\\') {
  4757.         bp++;
  4758.         if (*bp == '\n')
  4759.           ++*newlines;
  4760.         while (*bp == '\\' && bp[1] == '\n') {
  4761.           bp += 2;
  4762.         }
  4763.       } else if (*bp == '\n') {
  4764.         ++*newlines;
  4765.         if (quotec == '\'')
  4766.           break;
  4767.       }
  4768.     }
  4769.       }
  4770.       break;
  4771.     case ',':
  4772.       if ((*depthptr) == 0)
  4773.     return bp;
  4774.       break;
  4775.     }
  4776.     bp++;
  4777.   }
  4778.  
  4779.   return bp;
  4780. }
  4781.  
  4782. /* Discard comments and duplicate newlines
  4783.    in the string of length LENGTH at START,
  4784.    except inside of string constants.
  4785.    The string is copied into itself with its beginning staying fixed.  
  4786.  
  4787.    NEWLINES is the number of newlines that must be duplicated.
  4788.    We assume that that much extra space is available past the end
  4789.    of the string.  */
  4790.  
  4791. int
  4792. discard_comments (start, length, newlines)
  4793.      U_CHAR *start;
  4794.      int length;
  4795.      int newlines;
  4796. {
  4797.   register U_CHAR *ibp;
  4798.   register U_CHAR *obp;
  4799.   register U_CHAR *limit;
  4800.   register int c;
  4801.  
  4802.   /* If we have newlines to duplicate, copy everything
  4803.      that many characters up.  Then, in the second part,
  4804.      we will have room to insert the newlines
  4805.      while copying down.
  4806.      NEWLINES may actually be too large, because it counts
  4807.      newlines in string constants, and we don't duplicate those.
  4808.      But that does no harm.  */
  4809.   if (newlines > 0) {
  4810.     ibp = start + length;
  4811.     obp = ibp + newlines;
  4812.     limit = start;
  4813.     while (limit != ibp)
  4814.       *--obp = *--ibp;
  4815.   }
  4816.  
  4817.   ibp = start + newlines;
  4818.   limit = start + length + newlines;
  4819.   obp = start;
  4820.  
  4821.   while (ibp < limit) {
  4822.     *obp++ = c = *ibp++;
  4823.     switch (c) {
  4824.     case '\n':
  4825.       /* Duplicate the newline.  */
  4826.       *obp++ = '\n';
  4827.       break;
  4828.  
  4829.     case '/':
  4830.       if (*ibp == '\\' && ibp[1] == '\n')
  4831.     newline_fix (ibp);
  4832.       /* Delete any comment.  */
  4833.       if (cplusplus && ibp[0] == '/') {
  4834.     obp--;
  4835.     ibp++;
  4836.     while (ibp < limit && *ibp++ != '\n') ;
  4837.     break;
  4838.       }
  4839.       if (ibp[0] != '*' || ibp + 1 >= limit)
  4840.     break;
  4841.       obp--;
  4842.       ibp++;
  4843.       while (ibp + 1 < limit) {
  4844.     if (ibp[0] == '*'
  4845.         && ibp[1] == '\\' && ibp[2] == '\n')
  4846.       newline_fix (ibp + 1);
  4847.     if (ibp[0] == '*' && ibp[1] == '/')
  4848.       break;
  4849.     ibp++;
  4850.       }
  4851.       ibp += 2;
  4852.       break;
  4853.  
  4854.     case '\'':
  4855.     case '\"':
  4856.       /* Notice and skip strings, so that we don't
  4857.      think that comments start inside them,
  4858.      and so we don't duplicate newlines in them.  */
  4859.       {
  4860.     int quotec = c;
  4861.     while (ibp < limit) {
  4862.       *obp++ = c = *ibp++;
  4863.       if (c == quotec)
  4864.         break;
  4865.       if (c == '\n' && quotec == '\'')
  4866.         break;
  4867.       if (c == '\\' && ibp < limit) {
  4868.         while (*ibp == '\\' && ibp[1] == '\n')
  4869.           ibp += 2;
  4870.         *obp++ = *ibp++;
  4871.       }
  4872.     }
  4873.       }
  4874.       break;
  4875.     }
  4876.   }
  4877.  
  4878.   return obp - start;
  4879. }
  4880.  
  4881. /*
  4882.  * error - print error message and increment count of errors.
  4883.  */
  4884. error (msg, arg1, arg2, arg3)
  4885.      char *msg;
  4886. {
  4887.   int i;
  4888.   FILE_BUF *ip = NULL;
  4889.  
  4890.   for (i = indepth; i >= 0; i--)
  4891.     if (instack[i].fname != NULL) {
  4892.       ip = &instack[i];
  4893.       break;
  4894.     }
  4895.  
  4896.   if (ip != NULL)
  4897.     fprintf (stderr, "%s:%d: ", ip->fname, ip->lineno);
  4898.   fprintf (stderr, msg, arg1, arg2, arg3);
  4899.   fprintf (stderr, "\n");
  4900.   errors++;
  4901.   return 0;
  4902. }
  4903.  
  4904. /* Error including a message from `errno'.  */
  4905.  
  4906. error_from_errno (name)
  4907.      char *name;
  4908. {
  4909.   int i;
  4910.   FILE_BUF *ip = NULL;
  4911.   extern int errno, sys_nerr;
  4912.   extern char *sys_errlist[];
  4913.  
  4914.   for (i = indepth; i >= 0; i--)
  4915.     if (instack[i].fname != NULL) {
  4916.       ip = &instack[i];
  4917.       break;
  4918.     }
  4919.  
  4920.   if (ip != NULL)
  4921.     fprintf (stderr, "%s:%d: ", ip->fname, ip->lineno);
  4922.  
  4923. #ifdef atarist
  4924.   if ((errno > sys_nerr) && (errno < 0))
  4925. #else
  4926.   if (errno < sys_nerr)
  4927. #endif
  4928.     fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
  4929.   else
  4930.     fprintf (stderr, "%s: undocumented I/O error\n", name);
  4931.  
  4932.   errors++;
  4933.   return 0;
  4934. }
  4935.  
  4936. /* Print error message but don't count it.  */
  4937.  
  4938. warning (msg, arg1, arg2, arg3)
  4939.      char *msg;
  4940. {
  4941.   int i;
  4942.   FILE_BUF *ip = NULL;
  4943.  
  4944.   for (i = indepth; i >= 0; i--)
  4945.     if (instack[i].fname != NULL) {
  4946.       ip = &instack[i];
  4947.       break;
  4948.     }
  4949.  
  4950.   if (ip != NULL)
  4951.     fprintf (stderr, "%s:%d: ", ip->fname, ip->lineno);
  4952.   fprintf (stderr, "warning: ");
  4953.   fprintf (stderr, msg, arg1, arg2, arg3);
  4954.   fprintf (stderr, "\n");
  4955.   return 0;
  4956. }
  4957.  
  4958. error_with_line (line, msg, arg1, arg2, arg3)
  4959.      int line;
  4960.      char *msg;
  4961. {
  4962.   int i;
  4963.   FILE_BUF *ip = NULL;
  4964.  
  4965.   for (i = indepth; i >= 0; i--)
  4966.     if (instack[i].fname != NULL) {
  4967.       ip = &instack[i];
  4968.       break;
  4969.     }
  4970.  
  4971.   if (ip != NULL)
  4972.     fprintf (stderr, "%s:%d: ", ip->fname, line);
  4973.   fprintf (stderr, msg, arg1, arg2, arg3);
  4974.   fprintf (stderr, "\n");
  4975.   errors++;
  4976.   return 0;
  4977. }
  4978.  
  4979. /* Return the line at which an error occurred.
  4980.    The error is not necessarily associated with the current spot
  4981.    in the input stack, so LINE says where.  LINE will have been
  4982.    copied from ip->lineno for the current input level.
  4983.    If the current level is for a file, we return LINE.
  4984.    But if the current level is not for a file, LINE is meaningless.
  4985.    In that case, we return the lineno of the innermost file.  */
  4986. int
  4987. line_for_error (line)
  4988.      int line;
  4989. {
  4990.   int i;
  4991.   int line1 = line;
  4992.  
  4993.   for (i = indepth; i >= 0; ) {
  4994.     if (instack[i].fname != 0)
  4995.       return line1;
  4996.     i--;
  4997.     if (i < 0)
  4998.       return 0;
  4999.     line1 = instack[i].lineno;
  5000.   }
  5001. }
  5002.  
  5003. /*
  5004.  * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
  5005.  *
  5006.  * As things stand, nothing is ever placed in the output buffer to be
  5007.  * removed again except when it's KNOWN to be part of an identifier,
  5008.  * so flushing and moving down everything left, instead of expanding,
  5009.  * should work ok.
  5010.  */
  5011.  
  5012. int
  5013. grow_outbuf (obuf, needed)
  5014.      register FILE_BUF *obuf;
  5015.      register int needed;
  5016. {
  5017.   register U_CHAR *p;
  5018.   int minsize;
  5019.  
  5020.   if (obuf->length - (obuf->bufp - obuf->buf) > needed)
  5021.     return;
  5022.  
  5023.   /* Make it at least twice as big as it is now.  */
  5024.   obuf->length *= 2;
  5025.   /* Make it have at least 150% of the free space we will need.  */
  5026.   minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
  5027.   if (minsize > obuf->length)
  5028.     obuf->length = minsize;
  5029.  
  5030.   if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
  5031.     memory_full ();
  5032.  
  5033.   obuf->bufp = p + (obuf->bufp - obuf->buf);
  5034.   obuf->buf = p;
  5035. }
  5036.  
  5037. /* Symbol table for macro names and special symbols */
  5038.  
  5039. /*
  5040.  * install a name in the main hash table, even if it is already there.
  5041.  *   name stops with first non alphanumeric, except leading '#'.
  5042.  * caller must check against redefinition if that is desired.
  5043.  * delete_macro () removes things installed by install () in fifo order.
  5044.  * this is important because of the `defined' special symbol used
  5045.  * in #if, and also if pushdef/popdef directives are ever implemented.
  5046.  *
  5047.  * If LEN is >= 0, it is the length of the name.
  5048.  * Otherwise, compute the length by scanning the entire name.
  5049.  *
  5050.  * If HASH is >= 0, it is the precomputed hash code.
  5051.  * Otherwise, compute the hash code.
  5052.  */
  5053. HASHNODE *
  5054. install (name, len, type, value, hash)
  5055.      U_CHAR *name;
  5056.      int len;
  5057.      enum node_type type;
  5058.      int value;
  5059.      int hash;
  5060.         /* watch out here if sizeof (U_CHAR *) != sizeof (int) */
  5061. {
  5062.   register HASHNODE *hp;
  5063.   register int i, bucket;
  5064.   register U_CHAR *p, *q;
  5065.  
  5066.   if (len < 0) {
  5067.     p = name;
  5068.     while (is_idchar[*p])
  5069.       p++;
  5070.     len = p - name;
  5071.   }
  5072.  
  5073.   if (hash < 0)
  5074.     hash = hashf (name, len, HASHSIZE);
  5075.  
  5076.   i = sizeof (HASHNODE) + len + 1;
  5077.   hp = (HASHNODE *) xmalloc (i);
  5078.   bucket = hash;
  5079.   hp->bucket_hdr = &hashtab[bucket];
  5080.   hp->next = hashtab[bucket];
  5081.   hashtab[bucket] = hp;
  5082.   hp->prev = NULL;
  5083.   if (hp->next != NULL)
  5084.     hp->next->prev = hp;
  5085.   hp->type = type;
  5086.   hp->length = len;
  5087.   hp->value.ival = value;
  5088.   hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
  5089.   p = hp->name;
  5090.   q = name;
  5091.   for (i = 0; i < len; i++)
  5092.     *p++ = *q++;
  5093.   hp->name[len] = 0;
  5094.   return hp;
  5095. }
  5096.  
  5097. /*
  5098.  * find the most recent hash node for name name (ending with first
  5099.  * non-identifier char) installed by install
  5100.  *
  5101.  * If LEN is >= 0, it is the length of the name.
  5102.  * Otherwise, compute the length by scanning the entire name.
  5103.  *
  5104.  * If HASH is >= 0, it is the precomputed hash code.
  5105.  * Otherwise, compute the hash code.
  5106.  */
  5107. HASHNODE *
  5108. lookup (name, len, hash)
  5109.      U_CHAR *name;
  5110.      int len;
  5111.      int hash;
  5112. {
  5113.   register U_CHAR *bp;
  5114.   register HASHNODE *bucket;
  5115.  
  5116.   if (len < 0) {
  5117.     for (bp = name; is_idchar[*bp]; bp++) ;
  5118.     len = bp - name;
  5119.   }
  5120.  
  5121.   if (hash < 0)
  5122.     hash = hashf (name, len, HASHSIZE);
  5123.  
  5124.   bucket = hashtab[hash];
  5125.   while (bucket) {
  5126.     if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
  5127.       return bucket;
  5128.     bucket = bucket->next;
  5129.   }
  5130.   return NULL;
  5131. }
  5132.  
  5133. /*
  5134.  * Delete a hash node.  Some weirdness to free junk from macros.
  5135.  * More such weirdness will have to be added if you define more hash
  5136.  * types that need it.
  5137.  */
  5138.  
  5139. /* Note that the DEFINITION of a macro is removed from the hash table
  5140.    but its storage is not freed.  This would be a storage leak
  5141.    except that it is not reasonable to keep undefining and redefining
  5142.    large numbers of macros many times.
  5143.    In any case, this is necessary, because a macro can be #undef'd
  5144.    in the middle of reading the arguments to a call to it.
  5145.    If #undef freed the DEFINITION, that would crash.  */
  5146.  
  5147. delete_macro (hp)
  5148.      HASHNODE *hp;
  5149. {
  5150.  
  5151.   if (hp->prev != NULL)
  5152.     hp->prev->next = hp->next;
  5153.   if (hp->next != NULL)
  5154.     hp->next->prev = hp->prev;
  5155.  
  5156.   /* make sure that the bucket chain header that
  5157.      the deleted guy was on points to the right thing afterwards. */
  5158.   if (hp == *hp->bucket_hdr)
  5159.     *hp->bucket_hdr = hp->next;
  5160.  
  5161. #if 0
  5162.   if (hp->type == T_MACRO) {
  5163.     DEFINITION *d = hp->value.defn;
  5164.     struct reflist *ap, *nextap;
  5165.  
  5166.     for (ap = d->pattern; ap != NULL; ap = nextap) {
  5167.       nextap = ap->next;
  5168.       free (ap);
  5169.     }
  5170.     free (d);
  5171.   }
  5172. #endif
  5173.   free (hp);
  5174. }
  5175.  
  5176. /*
  5177.  * return hash function on name.  must be compatible with the one
  5178.  * computed a step at a time, elsewhere
  5179.  */
  5180. int
  5181. hashf (name, len, hashsize)
  5182.      register U_CHAR *name;
  5183.      register int len;
  5184.      int hashsize;
  5185. {
  5186.   register int r = 0;
  5187.  
  5188.   while (len--)
  5189.     r = HASHSTEP (r, *name++);
  5190.  
  5191.   return MAKE_POS (r) % hashsize;
  5192. }
  5193.  
  5194. /* Dump all macro definitions as #defines to stdout.  */
  5195.  
  5196. void
  5197. dump_all_macros ()
  5198. {
  5199.   int bucket;
  5200.  
  5201.   for (bucket = 0; bucket < HASHSIZE; bucket++) {
  5202.     register HASHNODE *hp;
  5203.  
  5204.     for (hp = hashtab[bucket]; hp; hp= hp->next) {
  5205.       if (hp->type == T_MACRO) {
  5206.     register DEFINITION *defn = hp->value.defn;
  5207.     struct reflist *ap;
  5208.     int offset;
  5209.     int concat;
  5210.  
  5211.  
  5212.     /* Print the definition of the macro HP.  */
  5213.  
  5214.     printf ("#define %s", hp->name);
  5215.     if (defn->nargs >= 0) {
  5216.       int i;
  5217.  
  5218.       printf ("(");
  5219.       for (i = 0; i < defn->nargs; i++) {
  5220.         dump_arg_n (defn, i);
  5221.         if (i + 1 < defn->nargs)
  5222.           printf (", ");
  5223.       }
  5224.       printf (")");
  5225.     }
  5226.  
  5227.     printf (" ");
  5228.  
  5229.     offset = 0;
  5230.     concat = 0;
  5231.     for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  5232.       dump_defn_1 (defn->expansion, offset, ap->nchars);
  5233.       if (ap->nchars != 0)
  5234.         concat = 0;
  5235.       offset += ap->nchars;
  5236.       if (ap->stringify)
  5237.         printf (" #");
  5238.       if (ap->raw_before && !concat)
  5239.         printf (" ## ");
  5240.       concat = 0;
  5241.       dump_arg_n (defn, ap->argno);
  5242.       if (ap->raw_after) {
  5243.         printf (" ## ");
  5244.         concat = 1;
  5245.       }
  5246.     }
  5247.     dump_defn_1 (defn->expansion, offset, defn->length - offset);
  5248.     printf ("\n");
  5249.       }
  5250.     }
  5251.   }
  5252. }
  5253.  
  5254. /* Output to stdout a substring of a macro definition.
  5255.    BASE is the beginning of the definition.
  5256.    Output characters START thru LENGTH.
  5257.    Discard newlines outside of strings, thus
  5258.    converting funny-space markers to ordinary spaces.  */
  5259.  
  5260. dump_defn_1 (base, start, length)
  5261.      U_CHAR *base;
  5262.      int start;
  5263.      int length;
  5264. {
  5265.   U_CHAR *p = base + start;
  5266.   U_CHAR *limit = base + start + length;
  5267.  
  5268.   while (p < limit) {
  5269.     if (*p != '\n')
  5270.       putchar (*p);
  5271.     else if (*p == '\"' || *p =='\'') {
  5272.       U_CHAR *p1 = skip_quoted_string (p, limit, 0, 0, 0, 0);
  5273.       fwrite (p, p1 - p, 1, stdout);
  5274.       p = p1 - 1;
  5275.     }
  5276.     p++;
  5277.   }
  5278. }
  5279.  
  5280. /* Print the name of argument number ARGNUM of macro definition DEFN.
  5281.    Recall that DEFN->argnames contains all the arg names
  5282.    concatenated in reverse order with comma-space in between.  */
  5283.  
  5284. dump_arg_n (defn, argnum)
  5285.      DEFINITION *defn;
  5286.      int argnum;
  5287. {
  5288.   register U_CHAR *p = defn->argnames;
  5289.   while (argnum + 1 < defn->nargs) {
  5290.     p = (U_CHAR *) index (p, ' ') + 1;
  5291.     argnum++;
  5292.   }
  5293.  
  5294.   while (*p && *p != ',') {
  5295.     putchar (*p);
  5296.     p++;
  5297.   }
  5298. }
  5299.  
  5300. /* Initialize syntactic classifications of characters.  */
  5301.  
  5302. initialize_char_syntax ()
  5303. {
  5304.   register int i;
  5305.  
  5306.   /*
  5307.    * Set up is_idchar and is_idstart tables.  These should be
  5308.    * faster than saying (is_alpha (c) || c == '_'), etc.
  5309.    * Must do set up these things before calling any routines tthat
  5310.    * refer to them.
  5311.    */
  5312.   for (i = 'a'; i <= 'z'; i++) {
  5313.     is_idchar[i - 'a' + 'A'] = 1;
  5314.     is_idchar[i] = 1;
  5315.     is_idstart[i - 'a' + 'A'] = 1;
  5316.     is_idstart[i] = 1;
  5317.   }
  5318.   for (i = '0'; i <= '9'; i++)
  5319.     is_idchar[i] = 1;
  5320.   is_idchar['_'] = 1;
  5321.   is_idstart['_'] = 1;
  5322.   is_idchar['$'] = dollars_in_ident;
  5323.   is_idstart['$'] = dollars_in_ident;
  5324.  
  5325.   /* horizontal space table */
  5326.   is_hor_space[' '] = 1;
  5327.   is_hor_space['\t'] = 1;
  5328.   is_hor_space['\v'] = 1;
  5329.   is_hor_space['\f'] = 1;
  5330.  
  5331.   is_space[' '] = 1;
  5332.   is_space['\t'] = 1;
  5333.   is_space['\v'] = 1;
  5334.   is_space['\f'] = 1;
  5335.   is_space['\n'] = 1;
  5336. }
  5337.  
  5338. /* Initialize the built-in macros.  */
  5339.  
  5340. initialize_builtins ()
  5341. {
  5342.   install ("__LINE__", -1, T_SPECLINE, 0, -1);
  5343.   install ("__DATE__", -1, T_DATE, 0, -1);
  5344.   install ("__FILE__", -1, T_FILE, 0, -1);
  5345.   install ("__BASE_FILE__", -1, T_BASE_FILE, 0, -1);
  5346.   install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, -1);
  5347.   install ("__VERSION__", -1, T_VERSION, 0, -1);
  5348.   install ("__TIME__", -1, T_TIME, 0, -1);
  5349.   if (!traditional)
  5350.     install ("__STDC__", -1, T_CONST, 1, -1);
  5351. /*  install ("__GNU__", -1, T_CONST, 1, -1);  */
  5352. /*  This is supplied using a -D by the compiler driver
  5353.     so that it is present only when truly compiling with GNU C.  */
  5354. }
  5355.  
  5356. /*
  5357.  * process a given definition string, for initialization
  5358.  * If STR is just an identifier, define it with value 1.
  5359.  * If STR has anything after the identifier, then it should
  5360.  * be identifier-space-definition.
  5361.  */
  5362. make_definition (str)
  5363.      U_CHAR *str;
  5364. {
  5365.   FILE_BUF *ip;
  5366.   struct directive *kt;
  5367.   U_CHAR *buf, *p;
  5368.  
  5369.   buf = str;
  5370.   p = str;
  5371.   while (is_idchar[*p]) p++;
  5372.   if (*p == 0) {
  5373.     buf = (U_CHAR *) alloca (p - buf + 4);
  5374.     strcpy ((char *)buf, str);
  5375.     strcat ((char *)buf, " 1");
  5376.   }
  5377.   
  5378.   ip = &instack[++indepth];
  5379.   ip->fname = "*Initialization*";
  5380.  
  5381.   ip->buf = ip->bufp = buf;
  5382.   ip->length = strlen (buf);
  5383.   ip->lineno = 1;
  5384.   ip->macro = 0;
  5385.   ip->free_ptr = 0;
  5386.   ip->if_stack = if_stack;
  5387.  
  5388.   for (kt = directive_table; kt->type != T_DEFINE; kt++)
  5389.     ;
  5390.  
  5391.   /* pass NULL as output ptr to do_define since we KNOW it never
  5392.      does any output.... */
  5393.   do_define (buf, buf + strlen (buf) , NULL, kt);
  5394.   --indepth;
  5395. }
  5396.  
  5397. /* JF, this does the work for the -U option */
  5398. make_undef (str)
  5399.      U_CHAR *str;
  5400. {
  5401.   FILE_BUF *ip;
  5402.   struct directive *kt;
  5403.  
  5404.   ip = &instack[++indepth];
  5405.   ip->fname = "*undef*";
  5406.  
  5407.   ip->buf = ip->bufp = str;
  5408.   ip->length = strlen (str);
  5409.   ip->lineno = 1;
  5410.   ip->macro = 0;
  5411.   ip->free_ptr = 0;
  5412.   ip->if_stack = if_stack;
  5413.  
  5414.   for (kt = directive_table; kt->type != T_UNDEF; kt++)
  5415.     ;
  5416.  
  5417.   do_undef (str,str + strlen (str) - 1, NULL, kt);
  5418.   --indepth;
  5419. }
  5420.  
  5421. /* Add output to `deps_buffer' for the -M switch.
  5422.    STRING points to the text to be output.
  5423.    SIZE is the number of bytes, or 0 meaning output until a null.
  5424.    If SIZE is nonzero, we break the line first, if it is long enough.  */
  5425.  
  5426. deps_output (string, size)
  5427.      char *string;
  5428.      int size;
  5429. {
  5430. #ifndef MAX_OUTPUT_COLUMNS
  5431. #define MAX_OUTPUT_COLUMNS 75
  5432. #endif
  5433.   if (size != 0 && deps_column != 0
  5434.       && size + deps_column > MAX_OUTPUT_COLUMNS) {
  5435.     deps_output ("\\\n  ", 0);
  5436.     deps_column = 0;
  5437.   }
  5438.  
  5439.   if (size == 0)
  5440.     size = strlen (string);
  5441.  
  5442.   if (deps_size + size + 1 > deps_allocated_size) {
  5443.     deps_allocated_size = deps_size + size + 50;
  5444.     deps_allocated_size *= 2;
  5445.     deps_buffer = (char *) xrealloc (deps_buffer, deps_allocated_size);
  5446.   }
  5447.   bcopy (string, &deps_buffer[deps_size], size);
  5448.   deps_size += size;
  5449.   deps_column += size;
  5450.   deps_buffer[deps_size] = 0;
  5451. }
  5452.  
  5453. #if (!(defined(atarist) || defined(atariminix) || defined(CROSSATARI)))
  5454. #ifndef BSD
  5455. #ifndef BSTRING
  5456.  
  5457. void
  5458. bzero (b, length)
  5459.      register char *b;
  5460.      register int length;
  5461. {
  5462. #ifdef VMS
  5463.   short zero = 0;
  5464.   long max_str = 65535;
  5465.  
  5466.   while (length > max_str) {
  5467.     (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
  5468.     length -= max_str;
  5469.     b += max_str;
  5470.   }
  5471.   (void) LIB$MOVC5 (&zero, &zero, &zero, &length, b);
  5472. #else
  5473.   while (length-- > 0)
  5474.     *b++ = 0;
  5475. #endif /* not VMS */
  5476. }
  5477.  
  5478. void
  5479. bcopy (b1, b2, length)
  5480.      register char *b1;
  5481.      register char *b2;
  5482.      register int length;
  5483. {
  5484. #ifdef VMS
  5485.   long max_str = 65535;
  5486.  
  5487.   while (length > max_str) {
  5488.     (void) LIB$MOVC3 (&max_str, b1, b2);
  5489.     length -= max_str;
  5490.     b1 += max_str;
  5491.     b2 += max_str;
  5492.   }
  5493.   (void) LIB$MOVC3 (&length, b1, b2);
  5494. #else
  5495.   while (length-- > 0)
  5496.     *b2++ = *b1++;
  5497. #endif /* not VMS */
  5498. }
  5499.  
  5500. int
  5501. bcmp (b1, b2, length)    /* This could be a macro! */
  5502.      register char *b1;
  5503.      register char *b2;
  5504.       register int length;
  5505. {
  5506. #ifdef VMS
  5507.    struct dsc$descriptor_s src1 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b1};
  5508.    struct dsc$descriptor_s src2 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b2};
  5509.  
  5510.    return STR$COMPARE (&src1, &src2);
  5511. #else
  5512.    while (length-- > 0)
  5513.      if (*b1++ != *b2++)
  5514.        return 1;
  5515.  
  5516.    return 0;
  5517. #endif /* not VMS */
  5518. }
  5519. #endif /* not BSTRING */
  5520. #endif /* not BSD */
  5521. #endif /* not ATARI */
  5522.  
  5523. void
  5524. fatal (str, arg)
  5525.      char *str, *arg;
  5526. {
  5527.   fprintf (stderr, "%s: ", progname);
  5528.   fprintf (stderr, str, arg);
  5529.   fprintf (stderr, "\n");
  5530.   exit (FATAL_EXIT_CODE);
  5531. }
  5532.  
  5533. /* More 'friendly' abort that prints the line and file.
  5534.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  5535.  
  5536. void
  5537. fancy_abort ()
  5538. {
  5539.   fatal ("Internal gcc abort.");
  5540. }
  5541.  
  5542. void
  5543. perror_with_name (name)
  5544.      char *name;
  5545. {
  5546.   extern int errno, sys_nerr;
  5547.   extern char *sys_errlist[];
  5548.  
  5549.   fprintf (stderr, "%s: ", progname);
  5550. #ifdef atarist
  5551.   if ((errno > sys_nerr) && (errno < 0))
  5552. #else
  5553.   if (errno < sys_nerr)
  5554. #endif
  5555.     fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
  5556.   else
  5557.     fprintf (stderr, "%s: undocumented I/O error\n", name);
  5558.   errors++;
  5559. }
  5560.  
  5561. void
  5562. pfatal_with_name (name)
  5563.      char *name;
  5564. {
  5565.   perror_with_name (name);
  5566. #ifdef VMS
  5567.   exit (vaxc$errno);
  5568. #else
  5569.   exit (FATAL_EXIT_CODE);
  5570. #endif
  5571. }
  5572.  
  5573.  
  5574. void
  5575. memory_full ()
  5576. {
  5577.   fatal ("Memory exhausted.");
  5578. }
  5579.  
  5580.  
  5581. char *
  5582. xmalloc (size)
  5583.      int size;
  5584. {
  5585.   extern char *malloc ();
  5586.   register char *ptr = malloc (size);
  5587.   if (ptr != 0) return (ptr);
  5588.   memory_full ();
  5589.   /*NOTREACHED*/
  5590. }
  5591.  
  5592. char *
  5593. xrealloc (old, size)
  5594.      char *old;
  5595.      int size;
  5596. {
  5597.   extern char *realloc ();
  5598.   register char *ptr = realloc (old, size);
  5599.   if (ptr != 0) return (ptr);
  5600.   memory_full ();
  5601.   /*NOTREACHED*/
  5602. }
  5603.  
  5604. char *
  5605. xcalloc (number, size)
  5606.      int number, size;
  5607. {
  5608.   extern char *malloc ();
  5609.   register int total = number * size;
  5610.   register char *ptr = malloc (total);
  5611.   if (ptr != 0) {
  5612.     if (total > 100)
  5613.       bzero (ptr, total);
  5614.     else {
  5615.       /* It's not too long, so loop, zeroing by longs.
  5616.      It must be safe because malloc values are always well aligned.  */
  5617.       register long *zp = (long *) ptr;
  5618.       register long *zl = (long *) (ptr + total - 4);
  5619.       register int i = total - 4;
  5620.       while (zp < zl)
  5621.     *zp++ = 0;
  5622.       if (i < 0)
  5623.     i = 0;
  5624.       while (i < total)
  5625.     ptr[i++] = 0;
  5626.     }
  5627.     return ptr;
  5628.   }
  5629.   memory_full ();
  5630.   /*NOTREACHED*/
  5631. }
  5632.  
  5633. char *
  5634. savestring (input)
  5635.      char *input;
  5636. {
  5637.   int size = strlen (input);
  5638.   char *output = xmalloc (size + 1);
  5639.   strcpy (output, input);
  5640.   return output;
  5641. }
  5642.  
  5643. /* Get the file-mode and data size of the file open on FD
  5644.    and store them in *MODE_POINTER and *SIZE_POINTER.  */
  5645.  
  5646. int
  5647. file_size_and_mode (fd, mode_pointer, size_pointer)
  5648.      int fd;
  5649.      int *mode_pointer;
  5650.      long int *size_pointer;
  5651. {
  5652.   struct stat sbuf;
  5653.  
  5654.   if (fstat (fd, &sbuf) < 0) return (-1);
  5655.   if (mode_pointer) *mode_pointer = sbuf.st_mode;
  5656.   if (size_pointer) *size_pointer = sbuf.st_size;
  5657.   return 0;
  5658. }
  5659.  
  5660. #ifdef    VMS
  5661.  
  5662. /* Under VMS we need to fix up the "include" specification
  5663.    filename so that everything following the 1st slash is
  5664.    changed into its correct VMS file specification. */
  5665.  
  5666. hack_vms_include_specification (fname)
  5667.      char *fname;
  5668. {
  5669.   register char *cp, *cp1, *cp2;
  5670.   char Local[512];
  5671.   extern char *index (), *rindex ();
  5672.  
  5673.   /* Ignore leading "./"s */
  5674.   while (fname[0] == '.' && fname[1] == '/')
  5675.     strcpy (fname, fname+2);
  5676.   /* Look for the boundary between the VMS and UNIX filespecs */
  5677.   cp = rindex (fname, ']');    /* Look for end of dirspec. */
  5678.   if (cp == 0) cp == rindex (fname, '>'); /* ... Ditto            */
  5679.   if (cp == 0) cp == rindex (fname, ':'); /* Look for end of devspec. */
  5680.   if (cp) {
  5681.     cp++;
  5682.   } else {
  5683.     cp = index (fname, '/');    /* Look for the "/" */
  5684.   }
  5685.   /* See if we found that 1st slash */
  5686.   if (cp == 0) return;        /* Nothing to do!!! */
  5687.   if (*cp != '/') return;    /* Nothing to do!!! */
  5688.   /* Point to the UNIX filename part (which needs to be fixed!) */
  5689.   cp1 = cp+1;
  5690.   /* If the directory spec is not rooted, we can just copy
  5691.      the UNIX filename part and we are done */
  5692.   if (((cp - fname) > 2)
  5693.       && ((cp[-1] == ']') || (cp[-1] == '>'))
  5694.       && (cp[-2] != '.')) {
  5695.     strcpy (cp, cp1);
  5696.     return;
  5697.   }
  5698.   /* If there are no other slashes then the filename will be
  5699.      in the "root" directory.  Otherwise, we need to add
  5700.      directory specifications. */
  5701.   if (index (cp1, '/') == 0) {
  5702.     /* Just add "[000000]" as the directory string */
  5703.     strcpy (Local, "[000000]");
  5704.     cp2 = Local + strlen (Local);
  5705.   } else {
  5706.     /* Open the directory specification */
  5707.     cp2 = Local;
  5708.     *cp2++ = '[';
  5709.     /* As long as there are still subdirectories to add, do them. */
  5710.     while (index (cp1, '/') != 0) {
  5711.       /* If this token is "." we can ignore it */
  5712.       if ((cp1[0] == '.') && (cp1[1] == '/')) {
  5713.     cp1 += 2;
  5714.     continue;
  5715.       }
  5716.       /* Add a subdirectory spec. */
  5717.       if (cp2 != Local+1) *cp2++ = '.';
  5718.       /* If this is ".." then the spec becomes "-" */
  5719.       if ((cp1[0] == '.') && (cp1[1] == '.') && (cp[2] == '/')) {
  5720.     /* Add "-" and skip the ".." */
  5721.     *cp2++ = '-';
  5722.     cp1 += 3;
  5723.     continue;
  5724.       }
  5725.       /* Copy the subdirectory */
  5726.       while (*cp1 != '/') *cp2++= *cp1++;
  5727.       cp1++;            /* Skip the "/" */
  5728.     }
  5729.     /* Close the directory specification */
  5730.     *cp2++ = ']';
  5731.   }
  5732.   /* Now add the filename */
  5733.   while (*cp1) *cp2++ = *cp1++;
  5734.   *cp2 = 0;
  5735.   /* Now append it to the original VMS spec. */
  5736.   strcpy (cp, Local);
  5737.   return;
  5738. }
  5739. #endif    /* VMS */
  5740.  
  5741. #ifdef    VMS
  5742.  
  5743. /* These are the read/write replacement routines for
  5744.    VAX-11 "C".  They make read/write behave enough
  5745.    like their UNIX counterparts that CCCP will work */
  5746.  
  5747. int
  5748. read (fd, buf, size)
  5749.      int fd;
  5750.      char *buf;
  5751.      int size;
  5752. {
  5753. #undef    read    /* Get back the REAL read routine */
  5754.   register int i;
  5755.   register int total = 0;
  5756.  
  5757.   /* Read until the buffer is exhausted */
  5758.   while (size > 0) {
  5759.     /* Limit each read to 32KB */
  5760.     i = (size > (32*1024)) ? (32*1024) : size;
  5761.     i = read (fd, buf, i);
  5762.     if (i <= 0) {
  5763.       if (i == 0) return (total);
  5764.       return(i);
  5765.     }
  5766.     /* Account for this read */
  5767.     total += i;
  5768.     buf += i;
  5769.     size -= i;
  5770.   }
  5771.   return (total);
  5772. }
  5773.  
  5774. int
  5775. write (fd, buf, size)
  5776.      int fd;
  5777.      char *buf;
  5778.      int size;
  5779. {
  5780. #undef    write    /* Get back the REAL write routine */
  5781.   int i;
  5782.   int j;
  5783.  
  5784.   /* Limit individual writes to 32Kb */
  5785.   i = size;
  5786.   while (i > 0) {
  5787.     j = (i > (32*1024)) ? (32*1024) : i;
  5788.     if (write (fd, buf, j) < 0) return (-1);
  5789.     /* Account for the data written */
  5790.     buf += j;
  5791.     i -= j;
  5792.   }
  5793.   return (size);
  5794. }
  5795.  
  5796. #endif /* VMS */
  5797.